找到重复字符的单词

Nem*_*emo 8 regex perl

想要在字典中搜索在第二个和最后一个位置具有相同字符的每个单词,并在某个中间位置搜索一次.

例子:

statement - has the "t" at the second, fourth and last place
severe = has "e" at 2,4,last
abbxb = "b" at 2,3,last
Run Code Online (Sandbox Code Playgroud)

错误

abab = "b" only 2 times not 3
abxxxbyyybzzzzb - "b" 4 times, not 3
Run Code Online (Sandbox Code Playgroud)

我的grep不起作用

my @ok = grep { /^(.)(.)[^\2]+(\2)[^\2]+(\2)$/ } @wordlist;
Run Code Online (Sandbox Code Playgroud)

例如

perl -nle 'print if /^(.)(.)[^\2]+(\2)[^\2]+(\2)$/' < /usr/share/dict/words
Run Code Online (Sandbox Code Playgroud)

打印例如

zarabanda
Run Code Online (Sandbox Code Playgroud)

怎么了.

什么应该是正确的正则表达式?

编辑:

如何捕捉封闭的群体?例如为了

statement - want cantupre: st(a)t(emen)t - for the later use

my $w1 = $1; my w2 = $2; or something like...
Run Code Online (Sandbox Code Playgroud)

ike*_*ami 13

(?:(?!STRING).)*STRING[^CHAR]*CHAR,那么你想要的是:

^.             # Ignore first char
(.)            # Capture second char
(?:(?!\1).)*   # Any number of chars that aren't the second char
\1             # Second char
(?:(?!\1).)*   # Any number of chars that aren't the second char
\1\z           # Second char at the end of the string.
Run Code Online (Sandbox Code Playgroud)

所以你得到:

perl -ne'print if /^. (.) (?:(?!\1).)* \1 (?:(?!\1).)* \1$/x' \
   /usr/share/dict/words
Run Code Online (Sandbox Code Playgroud)

要捕捉它们之间的内容,请在两者之间添加parens (?:(?!\1).)*.

perl -nle'print "$2:$3" if /^. (.) ((?:(?!\1).)*) \1 ((?:(?!\1).)*) \1\z/x' \
   /usr/share/dict/words
Run Code Online (Sandbox Code Playgroud)


anu*_*ava 5

这是应该适合您的正则表达式:

^.(.)(?=(?:.*?\1){2})(?!(?:.*?\1){3}).*?\1$
Run Code Online (Sandbox Code Playgroud)

现场演示:http://www.rubular.com/r/bEMgutE7t5