开发者

php regex is not escaped

开发者 https://www.devze.com 2022-12-27 19:25 出处:网络
what is a regex to find any text that has \'abc\' but does no开发者_如何学运维t have a \'\\\' before it. so it should match \'jfdgabc\' but not \'asd\\abc\'. basically so its not escaped.Use:

what is a regex to find any text that has 'abc' but does no开发者_如何学运维t have a '\' before it. so it should match 'jfdgabc' but not 'asd\abc'. basically so its not escaped.


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.


$str = 'jfdg\abc';

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


Try the regex:

(?<!\\)abc

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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号