Regex match whole words with special characters

Question:

I have a string (csv) like this:

american, american , latin-american, American, AMERICAN

I have tried this bamericanb but it matches all five words. It seems like it ignores some special characters like "-" and " ". It is also case-insensitive.

What it is supposed to do is ignore only the ",". It also has to be case-sensitive.

So the only match word was "american" (the first word on that string).

How can I achieve this?

Asked By: penk

||

Answers:

Try this:

(?:^|(?<=,s))american(?:(?=,)|$)

See regex demo

import re
txt = 'american, american , latin-american, American, AMERICAN'
re.findall('(?:^|(?<=,s))american(?:(?=,)|$)', txt)

//Output: ['american'] the first one.
Answered By: SaSkY
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.