用于分割所有未转义分号的正则表达式
我正在使用 php 的 preg_split 来根据 semi 分割字符串-冒号,但我需要它只在非转义分号上分割。
<?
$str = "abc;def\\;abc;def";
$arr = preg_split("/;/", $str);
print_r($arr);
?>
产生:
Array
(
[0] => abc
[1] => def\
[2] => abc
[3] => def
)
当我希望它产生时:
Array
(
[0] => abc
[1] => def\;abc
[2] => def
)
我尝试过 "/(^\\)?;/"
或 "/[^\\]?;/"
但它们都会产生错误。有什么想法吗?
I'm using php's preg_split to split up a string based on semi-colons, but I need it to only split on non-escaped semi-colons.
<?
$str = "abc;def\\;abc;def";
$arr = preg_split("/;/", $str);
print_r($arr);
?>
Produces:
Array
(
[0] => abc
[1] => def\
[2] => abc
[3] => def
)
When I want it to produce:
Array
(
[0] => abc
[1] => def\;abc
[2] => def
)
I've tried "/(^\\)?;/"
or "/[^\\]?;/"
but they both produce errors. Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这有效。
它输出:
您需要使用否定的lookbehind(阅读有关lookarounds)。想想“匹配所有';'除非前面有“\””。
This works.
It outputs:
You need to make use of a negative lookbehind (read about lookarounds). Think of "match all ';' unless preceed by a '\'".
我不太精通 PHP 正则表达式,但试试这个:
I am not really proficient with PHP regexes, but try this one:
既然巴特问:当然你也可以使用正则表达式来分割未转义的;并考虑转义转义字符。它只是变得有点混乱:
它的作用是采用“(除 \ 和 ; 之外的任何字符)或(\ 后跟任何字符)”的正则表达式,并允许任意数量的这些,后跟 ;或字符串的末尾。
我不确定 php 如何处理字符串中的 $ 和行尾字符,您可能需要设置一些正则表达式选项才能准确获得您想要的内容。
Since Bart asks: Of course you can also use regex to split on unescaped ; and take escaped escape characters into account. It just gets a bit messy:
What this does is to take a regular expression for “(any character except \ and ;) or (\ followed by any character)” and allow any number of those, followed by a ; or the end of the string.
I'm not sure how php handles $ and end-of-line characters within a string, you may need to set some regex options to get exactly what you want for those.