【问题标题】:regex: symbols can't repeat next to eachother正则表达式:符号不能相邻重复
【发布时间】:2014-01-11 20:52:07
【问题描述】:

我正在尝试制作正则表达式,它可以选择所有 a-z 且带有或不带有符号 ' 的单词。

  1. 单词至少需要 2 个字符
  2. 不能以 ' 符号开头
  3. 两个 ' 符号不能相邻
  4. 和“两个字符”的词不能以'符号结尾

我已经在那个正则表达式上工作了几个小时,但我无法让它工作:

/\b[a-z]([a-z(\')](?!\1))+\b/

它不起作用,我不知道为什么! (两个 ' 符号并排)

有什么想法吗?

【问题讨论】:

    标签: regex


    【解决方案1】:
    ([a-z](?:[a-z]|'(?!'))+[a-z']|[a-z]{2})
    

    Live @ RegExPal

    您可能不需要使用\b,因为正则表达式是贪婪的,并且会消耗整个单词。
    此版本无法使用 RegexPal 进行测试(无法识别lookbehind),但具有自定义单词边框:

    (?<![a-z'])([a-z](?:[a-z]|'(?!'))+[a-z']|[a-z]{2})(?![a-z'])
    

    【讨论】:

      【解决方案2】:

      这应该有效(免责声明:未经测试)

      /\b(?![a-z]{2}'\b)[a-z]((?!'')['a-z])+\b/
      

      你的不是因为你试图在字符类中嵌套一个带括号的表达式。这只会将() 添加到类中,它不会设置下一个\1 代码的值。

      (编辑)在aa' 上添加了约束。

      【讨论】:

        【解决方案3】:

        假设单词由空格分隔:

        (?:^|\s)((?:[a-z]{2})|(?:[a-z](?!.*'')[a-z']{2,}))(?:$|\s)
        

        在 perl 脚本中的作用:

        my $re = qr/(?:^|\s)((?:[a-z]{2})|(?:[a-z](?!.*'')[a-z']{2,}))(?:$|\s)/;
        while(<DATA>) {
            chomp;
            say (/$re/ ? "OK: $_" : "KO: $_");
        }
        __DATA__
        ab
        abc
        a'
        ab''
        abc'
        a''b
        :!ù
        

        输出:

        OK: ab
        OK: abc
        KO: a'
        OK: ab''
        OK: abc'
        KO: a''b
        KO: :!ù
        

        说明:

        The regular expression:
        
        (?-imsx:\b((?:[a-z]{2})|(?:[a-z](?!.*'')[a-z']{2,}))\b)
        
        matches as follows:
        
        NODE                     EXPLANATION
        ----------------------------------------------------------------------
        (?-imsx:                 group, but do not capture (case-sensitive)
                                 (with ^ and $ matching normally) (with . not
                                 matching \n) (matching whitespace and #
                                 normally):
        ----------------------------------------------------------------------
          \b                       the boundary between a word char (\w) and
                                   something that is not a word char
        ----------------------------------------------------------------------
          (                        group and capture to \1:
        ----------------------------------------------------------------------
            (?:                      group, but do not capture:
        ----------------------------------------------------------------------
              [a-z]{2}                 any character of: 'a' to 'z' (2 times)
        ----------------------------------------------------------------------
            )                        end of grouping
        ----------------------------------------------------------------------
           |                        OR
        ----------------------------------------------------------------------
            (?:                      group, but do not capture:
        ----------------------------------------------------------------------
              [a-z]                    any character of: 'a' to 'z'
        ----------------------------------------------------------------------
              (?!                      look ahead to see if there is not:
        ----------------------------------------------------------------------
                .*                       any character except \n (0 or more
                                         times (matching the most amount
                                         possible))
        ----------------------------------------------------------------------
                ''                       '\'\''
        ----------------------------------------------------------------------
              )                        end of look-ahead
        ----------------------------------------------------------------------
              [a-z']{2,}               any character of: 'a' to 'z', ''' (at
                                       least 2 times (matching the most
                                       amount possible))
        ----------------------------------------------------------------------
            )                        end of grouping
        ----------------------------------------------------------------------
          )                        end of \1
        ----------------------------------------------------------------------
          \b                       the boundary between a word char (\w) and
                                   something that is not a word char
        ----------------------------------------------------------------------
        )                        end of grouping
        ----------------------------------------------------------------------
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2020-04-11
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-05-12
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多