PHP正则表达式:在未转义的分隔符上分割 [英] PHP regex : split on unescaped delimiter

查看:154
本文介绍了PHP正则表达式:在未转义的分隔符上分割的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以使用以下代码来分割格式为 key:value; 的字符串:

  $ inside =key1:value1; key2:value2; key3:value3;; 
preg_match_all(/([^:] +):([^;] +); / s,$ inside,$ pairs);

我想做的是允许冒号和分号字符的出现引入一个转义字符的值,例如任何以反斜杠为前缀的冒号或分号都将被忽略,因此, \; 如果在相同的正则表达式中,奖励积分可以被保存在匹配的数组中,而不需要通过 str_replace 。感谢您提供的任何帮助。

解决方案

  preg_match_all(
' (#匹配和捕获...
(?:#任一:
\\\\。#一个转义的字符
|#或:
[^ \\ \\\:]#任何字符除了:或\
)+#一次或多次
)#捕获组1结束
:#匹配冒号
((?:\\\\\。。| [^ \\\\;])+);#第二部分与分号相同
/ x',
$ inside,$ pairs);

这样做。但是,它不会删除反斜杠。你不能在一个正则表达式本身;为此,您需要一个回调函数。



要匹配最终元素即使不以分隔符结尾,也可以更改; (?:; $)(与相同:)。并返回空的元素,并将 + 更改为 *


I am able to split strings in the format key:value; using the following code:

$inside = "key1:value1;key2:value2;key3:value3;";
preg_match_all("/([^:]+):([^;]+);/s", $inside, $pairs);

What I would like to do is allow for the occurrence of the colon and semi-colon character in the values by introducing an escape character e.g. \; any colon or semi-colon immediately preceded by a backslash would be ignored.

Bonus points if within the same regex, the escaped characters can then be stored in the array of matches unescaped without having to run everything through str_replace. Thanks for any help you can offer.

解决方案

preg_match_all(
    '/(                    # Match and capture...
     (?:                   # either:
      \\\\.                # an escaped character
     |                     # or:
      [^\\\\:]             # any character except : or \
     )+                    # one or more times
    )                      # End of capturing group 1
    :                      # Match a colon
    ((?:\\\\.|[^\\\\;])+); # Same for 2nd part with semicolons
    /x', 
    $inside, $pairs);

does this. It doesn't remove the backslashes, though. You can't do that in a regex itself; for this, you'd need a callback function.

To match the final element even if it doesn't end with a delimiter change the ; to (?:;|$) (same for the :). And to return empty elements as well change the + to a *.

这篇关于PHP正则表达式:在未转义的分隔符上分割的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆