David
David

Reputation: 2423

php regex is not escaped

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.

Upvotes: 0

Views: 46

Answers (3)

codaddict
codaddict

Reputation: 455092

Try the regex:

(?<!\\)abc

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

Upvotes: 0

zerkms
zerkms

Reputation: 254926

$str = 'jfdg\abc';

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

Upvotes: 0

cletus
cletus

Reputation: 625087

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.

Upvotes: 2

Related Questions