我在一个论坛上发现了这个正则表达式:
"/\+?\)\)(=.+?,.+?)?\](.+?)\[\/quote\](?!((.*?)\[\/quote\]))/s"
Run Code Online (Sandbox Code Playgroud)
正则表达式应该能够从字符串中提取数据.我会举个例子:
<?php
$string = ‘[quote=username]bla bla bla bla[/quote]’;
preg_match("/\+?\)\)(=.+?,.+?)?\](.+?)\[\/quote\](?!((.*?)\[\/quote\]))/s", $string, $match, null, 0);
print_r($match);
Run Code Online (Sandbox Code Playgroud)
...但是,没有任何内容返回$ match.我猜这个正则表达式有问题,因为它应该返回'username'和'bla bla bla bla'.我将在论坛中使用该函数作为引用函数.
在此先感谢,fischer
我会用一些更容易阅读的东西......
$string = '[quote=username]bla bla bla bla[/quote]';
preg_match('/\[quote=(.*?)\](.*?)\[\/quote\]/', $string, $matches);
print_r($matches);
Run Code Online (Sandbox Code Playgroud)
Array
(
[0] => [quote=username]bla bla bla bla[/quote]
[1] => username
[2] => bla bla bla bla
)
Run Code Online (Sandbox Code Playgroud)
我假设你的字符串将包含更多 - 因此懒惰的量词,没有开始和结束锚点.
另外,请确保您的字符串由单'
引号("
)或双引号()分隔,而不是反引号.反引号是一个shell_exec()
电话的语法糖.
我会尝试为你破译你原来的正则表达式......
/\+?\)\)(=.+?,.+?)?\](.+?)\[\/quote\](?!((.*?)\[\/quote\]))/s
/ -> start delimiter
\+? -> optional '+'
\) -> literal ')'
\) -> literal ')'
(=.+?,.+?)? -> not sure the reason for this (enlighten me?)
\] -> match literal ']'
(.+?) -> match one or more characters ungreedy
\[\/quote\] -> match ending '[/quote]'
(?! -> negative lookahead
((.*?) -> match 0 or more characters ungreedy
\[\/quote\] -> match ending '[/quote]'
))
/ -> ending delimiter
s -> case insensitive flag
Run Code Online (Sandbox Code Playgroud)