rvillablanca
rvillablanca

Reputation: 1646

Matching from a set of words

I need to find a regexp (in go) to match words from a word set like (sun|flower) but only separated words, for example "sun flower" should match both words, but "sunflower" or "sun-flower" should not.

My first approach was to use the regex \b(sun|flower)\b but for this case "sun-flower" both words are matching.

Trying with a regex like (?:^|\s+)(sun|flower)(?:\s+|$), "sun-flower" is not matching which is ok but when testing with "sun flower" only sun is matching.

I've seen that others languages like java or python have lookahead and lookbehind assertions and we can achieve this with a regex like

(?:^|(?<= ))(sun|flower)(?:(?= )|$)

But in go that assertions are not supported, so I wonder if there is a way to achieve this with go

Upvotes: 0

Views: 69

Answers (2)

djhaskin987
djhaskin987

Reputation: 10057

I posted an answer previously but then thought better.

Look-aheads and other such "fancy" things I have been able to get by without by using multiple passes on my data.

For example, why not simply split the string using whitespace and then looking for either sun or flower in the resultant slice of strings? the regexp involved would be much shorter (simply ^(sun|flower)$), and the code would be easier to maintain.

Upvotes: 0

Bohemian
Bohemian

Reputation: 424993

Remove dashes, then use the alternation wrapped in word boundaries:

\b(sun|flower)\b

Upvotes: 1

Related Questions