0

I do have comma separated data, like this:

"go,godown,goup,go,ago,agon,aerugos,gone,going,good,goof,goaway,nogo,go"

What would be the regular expression if go should match as complete word, not as part of any word. Like:

"go,godown,goup,go,ago,agon,aerugos,gone,going,good,goof,goaway,nogo,go"

Tried like this, but it's not exactly what i need:

(?<=,|^)(\s*)go(?=\s*,|\s*|$)

Thanks.

mrzasa
  • 22,895
  • 11
  • 56
  • 94
osmanz
  • 481
  • 5
  • 15
  • 1
    maybe `\bgo\b` https://regex101.com/r/GNJvcA/1 ? – mrzasa Mar 08 '18 at 23:37
  • `db.getCollection('videos').find({"synonym": {"$regex": "\bgo\b"}})` i am using it in pymongo query. it's not working as expected. would it be different here ? – osmanz Mar 09 '18 at 09:29
  • Try to use `//`, see example here: https://stackoverflow.com/a/32938354/580346 – mrzasa Mar 09 '18 at 09:45
  • 1
    Also: ready this answer and comments: https://stackoverflow.com/a/3483399/580346 – mrzasa Mar 09 '18 at 10:07

2 Answers2

1

Whenever you want to have a complete word match, use \b:

\bgo\b      -- 'go' word
AndreyS Scherbakov
  • 2,674
  • 2
  • 20
  • 27
1

You can use:

\bgo\b 

Demo

\b matches word boundary

mrzasa
  • 22,895
  • 11
  • 56
  • 94
  • thanks, it works well. but it matches in this case as well `...,go down,...` any suggestion to avoid it ? – osmanz Mar 09 '18 at 10:31
  • 3
    You can try: `(:?^|,)(go)(:?$|,)` https://regex101.com/r/GNJvcA/3. Do you have more cases like that? You had no spaces in the example in the question. – mrzasa Mar 09 '18 at 10:36