PHP preg_Match正则表达式改进

2022-03-29 00:00:00 regex php preg-match

您好,

我正在尝试扩大我当前在ShoutBox中使用的preg_Match的范围。我正在努力构建我当前的正则表达式,以获得所需的标识。请查看我正在使用的当前正则表达式,后跟有关我希望实现的匹配的一些信息。


当前正则表达式:

~f+(?:.+|s+)?r+(?:.+|s+)?e+(?:.+|s+)?d+(?:.+|)?~i

所需匹配信息:

[01]Lorem ipsum door弗雷德坐好。

  • 标识关键字。

[02]Lorem ipsum door$fred请坐。

  • 标识单个美元符号和关键字。

[03]Lorem ipsum door$of red请坐。

  • 标识单个美元符号,后跟单个字母数字字符和关键字。

[04]Lorem ipsum door$ooofred请坐。

  • 标识单个美元符号,后跟多个字母数字字符和关键字。

[05]Lorem ipsum door$ooofred请坐。

  • 标识多个美元符号,后跟多个字母数字字符和关键字。

[06]Lorem ipsum door$of red请坐。

  • 标识多个美元符号,后跟单个字母数字字符和关键字。

[07]Lorem ipsum door$o$oo$$of red请坐。

  • 标识美元符号和字母数字字符后跟关键字的任意组合。

[08]Lorem ipsum door$o$oo$of red请坐。

  • 空格分隔标识

[09]$of red请坐。

  • 标识为无前导空格

[10]Lorem ipsum Dolor$of red

  • 标识为不带尾随空格

[11]Lorem ipsum door$of red!

  • 用尾部符号标识

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


解决方案

对于这么小的正则表达式,这一定是我见过的最长的解释了:

if (preg_match('/(?<=^|s)(?:fred|$[$w]*fred)/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)
             # Assert position at a word boundary
      fred     # Match the characters "fred" literally
             # 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
             # Assert position at a word boundary
)
"

相关文章