正则表达式匹配3个或更多连续顺序字符和连续相同字符 [英] Regular Expression to match 3 or more Consecutive Sequential Characters and Consecutive Identical Characters
本文介绍了正则表达式匹配3个或更多连续顺序字符和连续相同字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要正则表达式来匹配以下情况。
I need regular expressions to match the below cases.
- 3个或更多连续的连续字符/数字;例如123,abc,789,pqr等。
- 3个或更多连续相同的字符/数字;例如111,aaa,bbb,222等。
推荐答案
我认为你不能在第一种情况下使用正则表达式。第二种情况很简单:
I don't think you can use regex for the first case. The second case is easy though:
Pattern pattern = Pattern.compile("([a-z\\d])\\1\\1", Pattern.CASE_INSENSITIVE);
由于 \\1
代表零件组匹配 1
这将匹配三个相同字符的任何序列,这些字符在 az
范围内或是数字( \d
)。
Since \\1
represents part matched by group 1
this will match any sequence of three identical characters that are either within the range a-z
or are digits (\d
).
这篇关于正则表达式匹配3个或更多连续顺序字符和连续相同字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文