13

I'm trying to match file paths that start with any string from a list. This is what I am using for that:

^/(dir1|dir2|dir3|tmp|dir4)/

I'm also trying to match all paths that start with /tmp/ but do not contain special after that.

This should match:

/tmp/subdir/filename.ext

But this should not:

/tmp/special/filename.ext

I can't seem to find a way to get this done. Any suggestions would be greatly appreciated.

Bogdan
  • 43,166
  • 12
  • 128
  • 129
  • 2
    A negative lookahead that is if your regex engine supported it `(?!special)`. – HamZa Nov 05 '13 at 12:49
  • possible duplicate of [Regex lookahead, lookbehind and atomic groups](http://stackoverflow.com/questions/2973436/regex-lookahead-lookbehind-and-atomic-groups) – HamZa Nov 05 '13 at 12:52

3 Answers3

9

Try ^(?i)/(dir1|dir2|dir3|tmp(?!\/(special))|dir4)/.*

(?i) = Case insesitivity this will match SpEcial, SPECial, SpEcIAL etc.

(?!\/(special)) = Negative lookahead for the '/special'

Srb1313711
  • 2,017
  • 5
  • 24
  • 35
  • This answer works just as well in my case, as the one from @grexter89 (since I need the match to be case sensitive), but your's is more comprehensive. Thanks – Bogdan Nov 05 '13 at 13:08
8

try this

^((?:dir1|dir2|dir3|dir4|tmp(?!/special)).*)$

Regular expression visualization

Debuggex Demo

bukart
  • 4,906
  • 2
  • 21
  • 40
1

try this ^(dir1|dir2|dir3|tmp(?!\/special)|dir4)

HamZa
  • 14,671
  • 11
  • 54
  • 75
grexter89
  • 1,091
  • 10
  • 23