【问题标题】:PHP preg_match Regular Expression ImprovementPHP preg_match 正则表达式改进
【发布时间】:2012-01-14 12:29:27
【问题描述】:

大家好

我正在尝试扩大我目前在喊话框中使用的 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 $$$ofred 坐等。

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

[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

  • 用尾随符号标识

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

【问题讨论】:

    标签: php regex preg-match


    【解决方案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
    )
    "
    

    【讨论】:

    • [红脸] 我想我不是最擅长解释事情 :) 非常感谢你,你的回答似乎很好地完成了这项工作。非常感谢!
    • @Matthew 待命,等待编辑。我正在改进它。可能有些情况我省略了:)
    • @Matthew 现在编辑的正则表达式与您要求的完全匹配。不多/不多。顺便说一句,欢迎你:)
    • 感谢修改,是否有机会在关键字中允许空格,即本例中的fred?
    • 那将是一个非常不同的更复杂的正则表达式。不过一切皆有可能。但是您可能应该为此提出另一个问题。
    【解决方案2】:
    /((\$[\w\$]*)?fred)/
    

    那个正则表达式有什么问题?

    我不确定我是否理解:

    (?:\.+|\s+)
    

    在你的正则表达式中。

    【讨论】:

    • 这对弗雷迪克鲁格失败了!而且您不希望这种情况发生:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-05
    • 2014-06-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多