搜索具有任何重复字符的单词

时间:2012-06-14 23:27:52

标签: regex perl

寻找perl one-liner什么会找到下一个模式的所有单词:

X(not_X_chrs)X(not_X_chrs)X    e.g. cyclic

对于一个字符,它很容易,例如为'a'

perl -nle 'print if /^a[^a]+a[^a]+a$/' < /usr/share/dict/web2

但我希望搜索 ANY 字符,因此,寻找一个正则表达式来查找所有字样,如:

azalea   #repeating a
baobab   #repeating b
cyclic   #c

依旧......

尝试了这个:

perl -nle 'print if m/^([a-z])[^$1]+$1[^$1]+$1$/i' </usr/share/dict/web2

但不起作用。

4 个答案:

答案 0 :(得分:6)

(?:(?!STRING).)

(?:STRING)

作为

[^CHAR]

CHAR

所以你可以使用

/
   ^
   (\pL)
   (?:
      (?:(?!\1).)+
      \1
   ){2}
   \z
/sx

答案 1 :(得分:3)

这是我能想到的最好的正则表达式:

^([a-z])((?:(?!\1).)+\1){2}$

RegexPal上进行了测试。

答案 2 :(得分:0)

您还可以使用具有原子非回溯组的延迟量词:

^(\w)(?>\w*?\1){2}$

只有当0个中间字符可以接受时才有效。

至少有1个字符你必须使用否定前瞻:

^(\w)(?>(?!\1)\w+?\1){2}$

答案 3 :(得分:0)

perlretut中,它表示您可以使用\g1对正则表达式(不是替换的正确部分)进行反向引用。这在5.14中有所改变。由于我这里只有5.12.2,所以我必须使用\1

因此,你的原始正则表达式对我来说很有用:

use strict; use warnings;
use 5.12.2;
use feature qw(say);
for (qw/ azalea baobab cyclic deadend teeeeeestest doesnotwork /) {
  say if m/^([a-z])[^\1]+\1[^\1]+\1$/i;
}

YAPE::Regex::Explain

查看
use YAPE::Regex::Explain;
print YAPE::Regex::Explain->new(qr/^([a-z])[^\1]+\1[^\1]+\1$/i)->explain();

的产率:

The regular expression:

(?i-msx:^([a-z])[^\1]+\1[^\1]+\1$)

matches as follows:


use YAPE::Regex::Explain;
print YAPE::Regex::Explain->new(qr/^([a-z])[^\1]+\1[^\1]+\1$/i)->explain();

NODE                     EXPLANATION
----------------------------------------------------------------------
(?i-msx:                 group, but do not capture (case-insensitive)
                         (with ^ and $ matching normally) (with . not
                         matching \n) (matching whitespace and #
                         normally):
----------------------------------------------------------------------
  ^                        the beginning of the string
----------------------------------------------------------------------
  (                        group and capture to \1:
----------------------------------------------------------------------
    [a-z]                    any character of: 'a' to 'z'
----------------------------------------------------------------------
  )                        end of \1
----------------------------------------------------------------------
  [^\1]+                   any character except: '\1' (1 or more
                           times (matching the most amount possible))
----------------------------------------------------------------------
  \1                       what was matched by capture \1
----------------------------------------------------------------------
  [^\1]+                   any character except: '\1' (1 or more
                           times (matching the most amount possible))
----------------------------------------------------------------------
  \1                       what was matched by capture \1
----------------------------------------------------------------------
  $                        before an optional \n, and the end of the
                           string
----------------------------------------------------------------------
)                        end of grouping
----------------------------------------------------------------------

修改:因此,您的单行代码为perl -e 'print if m/^([a-z])[^\1]+\1[^\1]+\1$/i'

另外请注意,如果您尝试perl -w -e 'print if m/(as)$1/',您会立即看到问题:

$ perl -w -e 'print if m/(a)$1/' asdf
Use of uninitialized value $1 in regexp compilation at -e line 1.
Use of uninitialized value $_ in pattern match (m//) at -e line 1.

我没想到的是为什么它匹配ololololo