正则表达式匹配任何重复超过 10 次的字符 [英] Regular expression to match any character being repeated more than 10 times

查看:57
本文介绍了正则表达式匹配任何重复超过 10 次的字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一个简单的正则表达式来匹配重复超过 10 次的相同字符.例如,如果我有一个散落着水平线的文档:

I'm looking for a simple regular expression to match the same character being repeated more than 10 or so times. So for example, if I have a document littered with horizontal lines:

=================================================

它会匹配 = 字符的行,因为它重复了 10 次以上.注意,我希望它适用于任何角色.

It will match the line of = characters because it is repeated more than 10 times. Note that I'd like this to work for any character.

推荐答案

你需要的正则表达式是 /(.)\1{9,}/.

The regex you need is /(.)\1{9,}/.

测试:

#!perl
use warnings;
use strict;
my $regex = qr/(.)\1{9,}/;
print "NO" if "abcdefghijklmno" =~ $regex;
print "YES" if "------------------------" =~ $regex;
print "YES" if "========================" =~ $regex;

这里 \1 被称为反向引用.它引用括号 (.) 之间的点 . 捕获的内容,然后 {9,} 要求九个或更多一样的性格.因此,这匹配十个或更多的任何单个字符.

Here the \1 is called a backreference. It references what is captured by the dot . between the brackets (.) and then the {9,} asks for nine or more of the same character. Thus this matches ten or more of any single character.

虽然上面的测试脚本是用 Perl 写的,但这是非常标准的正则表达式语法,应该适用于任何语言.在某些变体中,您可能需要使用更多反斜杠,例如Emacs 会让你在这里写 \(.\)\1\{9,\} .

Although the above test script is in Perl, this is very standard regex syntax and should work in any language. In some variants you might need to use more backslashes, e.g. Emacs would make you write \(.\)\1\{9,\} here.

如果整个字符串应该由 9 个或更多相同的字符组成,请在模式周围添加锚点:

If a whole string should consist of 9 or more identical characters, add anchors around the pattern:

my $regex = qr/^(.)\1{9,}$/;

这篇关于正则表达式匹配任何重复超过 10 次的字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆