开发者

PHP regex to find non-espaced letters

开发者 https://www.devze.com 2023-02-05 08:53 出处:网络
I want to replace F but not \\F. I\'ve tried the following code, without any luck. preg_replace(\"/[^\\\\]F/\", \"f\", $str开发者_StackOverflow社区);

I want to replace F but not \F.

I've tried the following code, without any luck.

preg_replace("/[^\\]F/", "f", $str开发者_StackOverflow社区);


Try this :

preg_replace("/(?<!\\\)F/", "f", $str);


This works.

$string = preg_replace('/([^\\\]|^)F/', '$1f', $string);

The reason there are three backslashes, is because the first one escapes the second one for the string, and that one escapes the last one for the regex. Here's a topic on another site about it: http://forums.devnetwork.net/viewtopic.php?t=125752

Update: Thanks to @Damp and @webbiedave

0

精彩评论

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