Bogdan
Bogdan

Reputation: 44526

Regex match string that starts with but does not include

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.

Upvotes: 14

Views: 23009

Answers (3)

bukart
bukart

Reputation: 4906

try this

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

Regular expression visualization

Debuggex Demo

Upvotes: 8

grexter89
grexter89

Reputation: 1102

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

Upvotes: 1

Srb1313711
Srb1313711

Reputation: 2047

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

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

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

Upvotes: 9

Related Questions