本文介绍了如何使用Perl正则表达式查找重复的字母?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在寻找一个可以找到重复字母的正则表达式。所以任何字母两次或更多,例如:
booooooot或abbott
pre>
我不知道我提前找的信。
这是我在面试中询问的问题,然后在面试中询问。
解决方案您可以找到任何字母,然后使用
\1
第二次找到相同的字母(或更多)。如果你只需要知道这封信,那么$ 1
将包含它。否则,您可以将第二个匹配连接到第一个匹配。my $ str =Foooooobar
$ str =〜/(\w)(\1 +)/;
print $ 1;
#prints'o'
print $ 1。 $ 2;
#prints'oooooo'
I am looking for a regex that will find repeating letters. So any letter twice or more, for example:
booooooot or abbott
I won't know the letter I am looking for ahead of time.
This is a question I was asked in interviews and then asked in interviews. Not so many people get it correct.
解决方案You can find any letter, then use
\1
to find that same letter a second time (or more). If you only need to know the letter, then$1
will contain it. Otherwise you can concatenate the second match onto the first.my $str = "Foooooobar"; $str =~ /(\w)(\1+)/; print $1; # prints 'o' print $1 . $2; # prints 'oooooo'
这篇关于如何使用Perl正则表达式查找重复的字母?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!