【问题标题】:Interpret this particular REGEX解释这个特定的正则表达式
【发布时间】:2009-07-13 10:19:11
【问题描述】:

前段时间我做了一个 REGEX 模式,但我不记得它的含义。对我来说,这是一种只写的语言:)

这是正则表达式:

"(?!^[0-9]*$)(?!^[a-zA-Z]*$)^([a-zA-Z0-9]{8,10})$"

我需要用简单的英语知道它是什么意思。

【问题讨论】:

  • 如果你看不懂正则表达式,你不应该写它们。
  • 如果您从未学习过正则表达式并且您突然维护包含它们的代码,那么这是一个合理的问题。

标签: python regex


【解决方案1】:
(?!^[0-9]*$)

不要只匹配数字,

(?!^[a-zA-Z]*$)

不要只匹配字母,

^([a-zA-Z0-9]{8,10})$

匹配长度为 8 到 10 个字符的字母和数字。

【讨论】:

    【解决方案2】:

    Perl(以及相应的Python)对(?!...) 部分说:

    一个零宽度的负前瞻断言。例如,/foo(?!bar)/ 匹配任何出现的 'foo' 且后面没有 'bar'。但是请注意,lookahead 和lookbehind 不是一回事。您不能将其用于后视。

    也就是说,

    (?!^[0-9]*$)
    

    表示:不匹配,如果字符串包含数字。^:行首/字符串,$:行尾/ string) 另一个相应的。

    您的正则表达式匹配任何包含数字和字母但不只是其中一个的字符串。

    干杯,

    更新:对于您未来的 RegExp 定制,请查看(?#...) 模式。它允许您在您的正则表达式中嵌入 cmets。还有一个修饰符,re.X,不过我不太喜欢这个。这是你的选择。

    【讨论】:

      【解决方案3】:

      RegexBuddy 说以下(!?!):

      (?!^[0-9]*$)(?!^[a-zA-Z]*$)^([a-zA-Z0-9]{8,10})$
      
      Options: ^ and $ match at line breaks
      
      Assert that it is impossible to match the regex below starting at this position (negative lookahead) «(?!^[0-9]*$)»
         Assert position at the beginning of a line (at beginning of the string or after a line break character) «^»
         Match a single character in the range between “0” and “9” «[0-9]*»
            Between zero and unlimited times, as many times as possible, giving back as needed (greedy) «*»
         Assert position at the end of a line (at the end of the string or before a line break character) «$»
      Assert that it is impossible to match the regex below starting at this position (negative lookahead) «(?!^[a-zA-Z]*$)»
         Assert position at the beginning of a line (at beginning of the string or after a line break character) «^»
         Match a single character present in the list below «[a-zA-Z]*»
            Between zero and unlimited times, as many times as possible, giving back as needed (greedy) «*»
            A character in the range between “a” and “z” «a-z»
            A character in the range between “A” and “Z” «A-Z»
         Assert position at the end of a line (at the end of the string or before a line break character) «$»
      Assert position at the beginning of a line (at beginning of the string or after a line break character) «^»
      Match the regular expression below and capture its match into backreference number 1 «([a-zA-Z0-9]{8,10})»
         Match a single character present in the list below «[a-zA-Z0-9]{8,10}»
            Between 8 and 10 times, as many times as possible, giving back as needed (greedy) «{8,10}»
            A character in the range between “a” and “z” «a-z»
            A character in the range between “A” and “Z” «A-Z»
            A character in the range between “0” and “9” «0-9»
      Assert position at the end of a line (at the end of the string or before a line break character) «$»  
      

      【讨论】:

      • 那应该有什么帮助? ;-)
      • 好问题,在应用中看起来更有帮助!
      • 感谢您向我介绍RegexBuddy。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-07-16
      • 2010-09-27
      • 1970-01-01
      • 2012-01-13
      • 1970-01-01
      • 1970-01-01
      • 2013-01-06
      相关资源
      最近更新 更多