你好全
我正在尝试扩大我目前在shoutbox中使用的preg_match的范围。我正在努力构建我当前的正则表达式,以获得所需的标识。请查看我正在使用的当前正则表达式,然后是一些关于我想实现的匹配的信息。
当前正则表达式:
~\bf+(?:\.+|\s+)?r+(?:\.+|\s+)?e+(?:\.+|\s+)?d+(?:\.+|)?\b~i
想要的匹配信息:
01 Lorem ipsum dolor fred同坐。
02 Lorem ipsum dolor $fred坐姿相同。
03 Lorem ipsum dolor $ofred坐席。
04 Lorem ipsum dolor $ooofred坐席。
05 $$$ooofred坐位。
06 Lorem ipsum dolor $$$ofred同坐。
07 Lorem ipsum dolor $o$oo$$$ofred同坐。
08 Lorem ipsum dolor $o$oo $$$ofred
破坏identification的
09 $ofred也是。
无前导空间的
10 Lorem ipsum dolor $ofred
无尾随空间的
11 $ofred!
识别
谢谢你的帮助,非常感谢。
发布于 2011-12-06 22:39:11
对于这么小的正则表达式,这一定是我见过的最长的解释:
if (preg_match('/(?<=^|\s)(?:\bfred\b|\$[$\w]*fred\b)/x', $subject, $regs)) {
$result = $regs[0];
}
解释:
"
(?<= # Assert that the regex below can be matched, with the match ending at this position (positive lookbehind)
# Match either the regular expression below (attempting the next alternative only if this one fails)
^ # Assert position at the beginning of the string
| # Or match regular expression number 2 below (the entire group fails if this one fails to match)
\s # Match a single character that is a “whitespace character” (spaces, tabs, line breaks, etc.)
)
(?: # Match the regular expression below
# Match either the regular expression below (attempting the next alternative only if this one fails)
\b # Assert position at a word boundary
fred # Match the characters “fred” literally
\b # Assert position at a word boundary
| # Or match regular expression number 2 below (the entire group fails if this one fails to match)
\$ # Match the character “$” literally
[$\w] # Match a single character present in the list below
# The character “$”
# A word character (letters, digits, etc.)
* # Between zero and unlimited times, as many times as possible, giving back as needed (greedy)
fred # Match the characters “fred” literally
\b # Assert position at a word boundary
)
"
发布于 2011-12-06 22:35:46
/((\$[\w\$]*)?fred)/
那个大霸王怎么了?
我不确定我是否理解:
(?:\.+|\s+)
在你的领地里。
https://stackoverflow.com/questions/8407796
复制相似问题