8

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

例子:

statement - has the "t" at the second, fourth and last place
severe = has "e" at 2,4,last
abbxb = "b" at 2,3,last

错误的

abab = "b" only 2 times not 3
abxxxbyyybzzzzb - "b" 4 times, not 3

我的 grep 不工作

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

例如

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

打印例如

zarabanda

怎么了。

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

编辑:

以及如何捕获封闭的组?例如对于

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

my $w1 = $1; my w2 = $2; or something like...
4

4 回答 4

13

(?:(?!STRING).)*is STRINGas [^CHAR]*is to 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.

所以你得到:

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

要捕获介于两者之间的内容,请在两者周围添加括号(?:(?!\1).)*

perl -nle'print "$2:$3" if /^. (.) ((?:(?!\1).)*) \1 ((?:(?!\1).)*) \1\z/x' \
   /usr/share/dict/words
于 2013-06-02T00:38:35.017 回答
5

这是应该为您工作的正则表达式:

^.(.)(?=(?:.*?\1){2})(?!(?:.*?\1){3}).*?\1$

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

于 2013-06-02T00:27:54.943 回答
1

使用前瞻:

/^.(.)(?!(?:.*\1){3}).*\1(.*)\1$/

意义:

/^.(.)(?!(?:.*\1){3})  # capture the second character if it is not
                       # repeated more than twice after the 2nd position
.*\1(.*)\1$              # match captured char 2 times the last one at the end
于 2013-06-02T00:33:28.707 回答
1
my @ok = grep {/^.(\w)/; /^.$1[^$1]*?$1[^$1]*$1$/ } @wordlist;
于 2013-06-02T00:41:39.963 回答