PHP preg_match Regular Expression Improvement

感情迁移 提交于 2019-12-24 09:52:32

问题


Hello All,

I am trying to widen the scope on a preg_match I currently use in a shoutbox. I am struggling to build on my current regular expression to obtain the desired identification. Please see the current regex I am using followed by some information on the matches I would like to achieve.


Current regular expression:

~\bf+(?:\.+|\s+)?r+(?:\.+|\s+)?e+(?:\.+|\s+)?d+(?:\.+|)?\b~i

Desired match info:

[01] Lorem ipsum dolor fred sit amet.

  • Identify key word.

[02] Lorem ipsum dolor $fred sit amet.

  • Identify single dollar symbol and key word.

[03] Lorem ipsum dolor $ofred sit amet.

  • Identify single dollar symbol followed by a single alphanumeric character and key word.

[04] Lorem ipsum dolor $ooofred sit amet.

  • Identify single dollar symbol followed by multiple alphanumeric characters and key word.

[05] Lorem ipsum dolor $$$ooofred sit amet.

  • Identify multiple dollar symbols followed by multiple alphanumeric characters and key word.

[06] Lorem ipsum dolor $$$ofred sit amet.

  • Identify multiple dollar symbols followed by a single alphanumeric character and key word.

[07] Lorem ipsum dolor $o$oo$$$ofred sit amet.

  • Identify any combination of dollar symbols and alphanumeric characters followed by key word.

[08] Lorem ipsum dolor $o$oo $$$ofred sit amet.

  • Spaces break the identification

[09] $ofred sit amet.

  • Identified with no leading spaces

[10] Lorem ipsum dolor $ofred

  • Identified with no trailing spaces

[11] Lorem ipsum dolor $ofred!

  • Identified with trailing symbols

Thank you for any help, it's much appreciated.


回答1:


This has to be the longest explanation I have ever seen for a regex that is so small:

if (preg_match('/(?<=^|\s)(?:\bfred\b|\$[$\w]*fred\b)/x', $subject, $regs)) {
    $result = $regs[0];
}

Explanation:

"
(?<=           # 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
)
"



回答2:


/((\$[\w\$]*)?fred)/

What's wrong with that regex?

I'm not sure I understand the significance of:

(?:\.+|\s+)

In your regex.



来源:https://stackoverflow.com/questions/8407796/php-preg-match-regular-expression-improvement

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!