Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex to match against something that is not a specific substring

I am looking for a regex that will match a string that starts with one substring and does not end with a certain substring.

Example:

// Updated to be correct, thanks @Apocalisp
^foo.*(?<!bar)$

Should match anything that starts with "foo" and doesn't end with "bar". I know about the [^...] syntax, but I can't find anything that will do that for a string instead of single characters.

I am specifically trying to do this for Java's regex, but I've run into this before so answers for other regex engines would be great too.

Thanks to @Kibbee for verifying that this works in C# as well.

like image 417
John Meagher Avatar asked Sep 04 '08 01:09

John Meagher


1 Answers

I think in this case you want negative lookbehind, like so:

foo.*(?<!bar)
like image 93
Apocalisp Avatar answered Oct 13 '22 14:10

Apocalisp