Hello All ,
我正在尝试扩大目前在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] Lorem ipsum dolor $$$ ooofred 坐下来。
[06] Lorem ipsum dolor $$$ ofredred 坐好。
[07] Lorem ipsum dolor $ o $ oo $$$ ofred 坐下来。
[08] Lorem ipsum dolor $ o $ oo $$$ ofred 坐好。
[09] $ ofred 坐下来。
[10] Lorem ipsum dolor $ ofred
[11] Lorem ipsum dolor $ ofred !
感谢您的帮助,非常感谢。
答案 0 :(得分:1)
/((\$[\w\$]*)?fred)/
正则表达式有什么问题?
我不确定我理解的重要性:
(?:\.+|\s+)
在你的正则表达式中。
答案 1 :(得分:1)
这必须是我见过的最长的正则表达式的解释:
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
)
"