我正在尝试使用两个不同的模式来匹配字符串。
我的源字符串是这样的:
Text, white-spaces, new lines and more text then ^^^^<customtag>
我需要得到一个(第二个)组,该组将捕获一个插入符号,或者不捕获任何一个格式化的类似HTML的标记。因此,第一组将捕获其他任何内容。
这意味着上面的字符串应输出以下内容:
(Group 1)
Text, white-spaces, new lines and more text then ^^^
(Group 2)
^<customtag>
在源字符串中,插入号可以为一,无或最多两千。
我需要一个匹配最后一个插入符以外所有插入符的良好模式。
下面的代码是我尝试的。
preg_match_all('/([\s\S]*\^*)(\^?<\w+>)$/', $string, $matches);
请注意:我使用了[\s\S]
代替点来匹配任何字符,空格和换行符。
您可以遵循以下正则表达式:
(?s)(.*)((\^|(?<!\^))<[^>]+>)
PHP代码:
preg_match_all('/(?s)(.*)((\^|(?<!\^))<[^>]+>)/', $string, $matches);
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句