0

I want to match the string path but not the string os.path. How do I go about that ? Tried (?!(os\.path))\bpath\b but I still get all os.pathS

Community
  • 1
  • 1
Mr_and_Mrs_D
  • 32,208
  • 39
  • 178
  • 361

2 Answers2

1

You can use a look-behind based regex, like

(?<!os\.)\bpath\b

This basically matches the exact word path and ensures that it is not preceded by os. If you want to avoid similar constructs, like sys.path or xx.path you could use (?<!\w\.) as look-behind instead.

See the regex101 demo.

Sebastian Proske
  • 8,255
  • 2
  • 28
  • 37
0

If you want to skip any path starting with . try

(?<!\.)path

This will skip sys.path or os.path but will match path.

e.g. if test strings is if path and not os.path.sep in path,

match will be:

if path and not os.path.sep in path

See demo at regex101

Saleem
  • 8,728
  • 2
  • 20
  • 34