用于分割所有未转义分号的正则表达式

发布于 2024-08-18 10:39:20 字数 582 浏览 3 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

£冰雨忧蓝° 2024-08-25 10:39:20

这有效。

<?
  $str = "abc;def\;abc;def";
  $arr = preg_split('/(?<!\\\);/', $str);
  print_r($arr);
?>

它输出:

Array
(
    [0] => abc
    [1] => def\;abc
    [2] => def
) 

您需要使用否定的lookbehind(阅读有关lookarounds)。想想“匹配所有';'除非前面有“\””。

This works.

<?
  $str = "abc;def\;abc;def";
  $arr = preg_split('/(?<!\\\);/', $str);
  print_r($arr);
?>

It outputs:

Array
(
    [0] => abc
    [1] => def\;abc
    [2] => def
) 

You need to make use of a negative lookbehind (read about lookarounds). Think of "match all ';' unless preceed by a '\'".

你与清晨阳光 2024-08-25 10:39:20

我不太精通 PHP 正则表达式,但试试这个:

/(?<!\\);/

I am not really proficient with PHP regexes, but try this one:

/(?<!\\);/
夏九 2024-08-25 10:39:20

既然巴特问:当然你也可以使用正则表达式来分割未转义的;并考虑转义转义字符。它只是变得有点混乱:

<?
  $str = "abc;def\;abc\\\\;def";
  preg_match_all('/((?:[^\\\\;]|\\\.)*)(?:;|$)/', $str, $arr);
  print_r($arr);
?>

Array
(
  [0] => Array
      (
          [0] => abc;
          [1] => def\;abc\\;
          [2] => def
      )

  [1] => Array
      (
          [0] => abc
          [1] => def\;abc\\
          [2] => def
      )
)

它的作用是采用“(除 \ 和 ; 之外的任何字符)或(\ 后跟任何字符)”的正则表达式,并允许任意数量的这些,后跟 ;或字符串的末尾。

我不确定 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:

<?
  $str = "abc;def\;abc\\\\;def";
  preg_match_all('/((?:[^\\\\;]|\\\.)*)(?:;|$)/', $str, $arr);
  print_r($arr);
?>

Array
(
  [0] => Array
      (
          [0] => abc;
          [1] => def\;abc\\;
          [2] => def
      )

  [1] => Array
      (
          [0] => abc
          [1] => def\;abc\\
          [2] => def
      )
)

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文