PHP preg_match正则表达式改进

时间:2011-12-06 22:27:15

标签: php regex preg-match

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 坐好。

  • Spaces打破了身份

[09] $ ofred 坐下来。

  • 标识为没有前导空格

[10] Lorem ipsum dolor $ ofred

  • 标识为没有尾随空格

[11] Lorem ipsum dolor $ ofred

  • 使用尾随符号标识

感谢您的帮助,非常感谢。

2 个答案:

答案 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
)
"