【问题标题】:Regular Expression that matches a word, or nothing匹配一个单词的正则表达式,或者什么都不匹配
【发布时间】:2014-02-07 05:53:25
【问题描述】:

我真的很难在这上面贴上标签,这可能是我无法通过搜索找到所需内容的原因。

我希望匹配以下内容:

  • 自动回复
  • 自动回复
  • 自动回复

我使用的平台不允许指定不区分大小写的搜索。我尝试了以下正则表达式:

.*[aA]uto(?:matic)[ ]*[rR]eply.*

认为(?:matic) 会导致我的表达式匹配AutoAutomatic。但是,它只匹配Automatic

  • 我做错了什么?
  • 这里的正确术语是什么?

这是使用 Perl 的正则表达式引擎(我认为是 PCRE,但我不确定)。

【问题讨论】:

  • 如果这是一个Perl正则表达式,你不需要.*在开始和结束,因为默认是一个正则表达式可以匹配字符串的任何部分,不需要匹配所有的.您可以使用 (?^i: ... ) 扩展模式向正则表达式添加修饰符。
  • @TLP 我会试试看。我所拥有的只是 OTRS 中 Web 表单上的一个输入字段,它允许我指定一个正则表达式。我还没有深入研究源代码以查看它对输入的作用,但您所说的可能会正常工作。谢谢。
  • perl != PCRE, Check the difference。另外,您的平台如何不允许不区分大小写?您是否尝试过在您的正则表达式中设置 i 修饰符,例如 (?i)auto(?:matic)?\s*reply
  • 仅供参考,PCRE 和 Perl 正则表达式引擎是不同的,但大多是兼容的正则表达式系统。现代 Perl 甚至支持可插入的正则表达式引擎。请参阅pcre.orgperldoc.perl.org/perlre.html 了解更多信息。
  • 感谢您的信息。我正在使用 OTRS,它提供了一个用于提交正则表达式的网络表单。我不确定在那里之后它会做什么。我知道 OTRS 是用 Perl 编写的。我发现关于正在使用哪种类型的正则表达式引擎,或者在输入值后它对值做了什么的文档为零。

标签: regex perl pcre


【解决方案1】:

您可以将此正则表达式与行开始/结束锚点一起使用:

^[aA]uto(?:matic)? *[rR]eply$

说明:

^ assert position at start of the string
[aA] match a single character present in the list below
aA a single character in the list aA literally (case sensitive)
uto matches the characters uto literally (case sensitive)
(?:matic)? Non-capturing group
Quantifier: Between zero and one time, as many times as possible, giving back as needed 
[greedy]
matic matches the characters matic literally (case sensitive)
 * matches the character   literally
Quantifier: Between zero and unlimited times, as many times as possible, giving back 
as needed [greedy]
[rR] match a single character present in the list below
rR a single character in the list rR literally (case sensitive)
eply matches the characters eply literally (case sensitive)
$ assert position at end of the string

【讨论】:

    【解决方案2】:

    (?:...) 是正则表达式模式,就像(...) 是算术:它只是覆盖优先级。

     ab|cd        # Matches ab or cd
     a(?:b|c)d    # Matches abd or acd
    

    ? 量词使匹配成为可选。

     a?           # Matches a or an empty string
     abc?d        # Matches abcd or abd
     a(?:bc)?d    # Matches abcd or ad
    

    你想要

    (?:matic)?
    

    没有不必要的前导和尾随.*,我们得到以下内容:

    /[aA]uto(?:matic)?[ ]*[rR]eply/
    

    正如@adamdc78 指出的那样,它与AutoReply 匹配。这可以通过以下方式避免:

    /[aA]uto(?:matic[ ]*|[ ]+)[rR]eply/
    

    /[aA]uto(?:matic|[ ])[ ]*[rR]eply/
    

    【讨论】:

    • ?: 有必要吗?我可以改用(matic)? 吗?
    • @crush 你可以,但是matic 将被保存在一个捕获组中;除非您确实需要 matic 的值,否则捕获组没有意义。
    • @HunterMcMillen 感谢您的澄清。这就是我的想法,但不确定。 ? 在捕获组之后有技术名称吗?
    • @crush 是一个量词。
    • (matic)? 不必要地捕获。你可以这样做,但这会很浪费。在我的回答中将“修饰符”更改为“量词”。
    【解决方案3】:

    这应该可行:

    /.*[aA]uto(?:matic)? *[rR]eply/
    

    你只是在(?:matic)之后错过了?

    【讨论】:

      【解决方案4】:
      [Aa]uto(?:matic ?| )[Rr]eply
      

      这假设您不希望 AutoReply 成为有效命中。

      您只是缺少正则表达式中的optional ("?")。如果您希望在回复后匹配整行,那么在末尾包含 .* 就可以了,但您的问题没有具体说明您要查找的内容。

      【讨论】:

        【解决方案5】:

        略有不同。结果一样。

        m/([aA]uto(matic)? ?[rR]eply)/
        

        测试对象:

        Some other stuff....
            Auto Reply
            Automatic Reply
            AutomaticReply
        
        Now some similar stuff that shouldn't match (auto).
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2014-11-18
          • 2021-08-08
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2023-04-01
          • 1970-01-01
          • 2010-11-15
          相关资源
          最近更新 更多