Community Spring Cleaning week is here! Join your fellow Maveryx in digging through your old posts and marking comments on them as solved. Learn more here!

Alteryx Designer Desktop Discussions

Find answers, ask questions, and share expertise about Alteryx Designer Desktop and Intelligence Suite.
SOLVED

Extract the first N words

stevenlsj789
8 - Asteroid

Hi all- I am trying to design a marco that can help users to extract the first N words. But struggle with the REGEX here. In my example below, I have "(^(?:\S+\s+\n?){1,3})" so it should show the first 3 words, but instead it will only show the first 2 words. Any idea why? I attached the workflow below. Thanks! 

4 REPLIES 4
lmorrell
11 - Bolide

Hi @stevenlsj789 

 

This is a fun one for sure! The RegEx statement is returning:

A string of one or more non-whitespace characters FOLLOWED BY a string of one or more whitespace characters POTENTIALLY FOLLOWED BY a newline character. 

 

The final character for the final word is not a space, therefore the RegEx is not capturing that last word. 

 

I'm sure there are many solutions to this, but one solution is the below RegEx that removes the necessity for a capture word to end with a space by allowing 0 or more spaces following the non-whitespace characters.

(^(?:\S+\s*\n?){1,3})

 

Hope this helps! 

Thableaus
17 - Castor
17 - Castor

Hi @stevenlsj789 

 

Here's a creative way to do it

 

quickext.PNG

 

- Address a RecordID to each row

- Use Tokenize REGEX to split words using the expression (\b\w+\b)

- Use Sample Tool and apply your numeric drop down to change how many words you want.

- Group by RecordID and concatenate separating by spaces (\s)

- Drop the RecordID field

 

This is just one way without complicating it too much (it uses more tools, but it's simple to understand)

 

WF attached.

 

Cheers,

csmith11
11 - Bolide

This looks like it was an awesome macro project! A very simple approach may have just been to use the GetWord(String, 1) and applied this to all value. The Macro could be then enable to dynamically update the number of words required. Just posting here in case other communities members find this helpful.

rzdodson
12 - Quasar

This is one way to do it. Put this in a Regex tool.

(^(?:\S+\s*\n?){1,3})

 

Solution.png

 

 

Labels