tags:

views:

25

answers:

3

what is a regex to find any text that has 'abc' but does not have a '\' before it. so it should match 'jfdgabc' but not 'asd\abc'. basically so its not escaped.

+2  A: 

Use:

(?<!\\)abc

This is a negative lookbehind. Basically this is saying: find me the string "abc" that is not preceded by a backslash.

The one problem with this is that if you want to allow escaping of backslashes. For example:

123\\abcdef

(ie the backslash is escaped) then it gets a little trickier.

cletus
but you forgot one more \
zerkms
yeh i added the extra backslash but thanks
David
A: 
$str = 'jfdg\abc';

var_dump(preg_match('#(?<!\\\)abc#', $str));
zerkms
A: 

Try the regex:

(?<!\\)abc

It matches a abc only if its not preceded by a \

codaddict