正则表达式使用 Java 模式匹配字符串中的四个重复字母
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2622776/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Regex to match four repeated letters in a string using a Java pattern
提问by Anonymous user
I want to match something like aaaa, aaaad, adjjjjk. Something like ([a-z])\1+ was used to match the repeated characters, but I am not able to figure this out for four letters.
我想匹配诸如 aaaa、aaaad、adjjjjk 之类的东西。像 ([az])\1+ 这样的东西被用来匹配重复的字符,但我无法弄清楚四个字母。
采纳答案by polygenelubricants
Not knowing about the finite repetition syntax, your own problem solving skill should lead you to this:
不知道有限重复语法,你自己的问题解决技巧应该引导你:
([a-z])
Obviously it's not pretty, but:
显然它不漂亮,但是:
- It works
- It exercises your own problem solving skill
- It may lead you to deeper understanding of concepts
- In this case, knowing the desugared form of the finite repetition syntax
- 有用
- 它锻炼了你自己解决问题的能力
- 它可能会让你更深入地理解概念
- 在这种情况下,知道有限重复语法的脱糖形式
I have a concern:
"ffffffff".matches("([a-z])\\1{3,}") = true
"fffffasdf".matches("([a-z])\\1{3,}") = false
"asdffffffasdf".matches("([a-z])\\1{3,}") = false
What can I do for the bottom two?
我有一个顾虑:
"ffffffff".matches("([a-z])\\1{3,}") = true
"fffffasdf".matches("([a-z])\\1{3,}") = false
"asdffffffasdf".matches("([a-z])\\1{3,}") = false
我能为底部的两个做什么?
The problem is that in Java, matches
need to match the whole string; it is as if the pattern is surrounded by ^
and $
.
问题是在Java中,matches
需要匹配整个字符串;就好像图案被^
和包围$
。
Unfortunately there is no String.containsPattern(String regex)
, but you can always use this trick of surrounding the pattern with .*
:
不幸的是没有String.containsPattern(String regex)
,但你总是可以使用这种围绕模式的技巧.*
:
"asdfffffffffasf".matches(".*([a-z])\1{3,}.*") // true!
// ^^ ^^
回答by Mark Byers
You want to match a single character and then that character repeated three more times:
您想匹配单个字符,然后该字符再重复 3 次:
([a-z]){3}
Note: In Java you need to escape the backslashes inside your regular expressions.
注意:在 Java 中,您需要对正则表达式中的反斜杠进行转义。
Update: The reason why it isn't doing what you want is because you are using the method matches
which requires that the string exactly matches the regular expression, not just that it contains the regular expression. To check for containment you should instead use the Matcher
class. Here is some example code:
更新:它没有做你想做的事情的原因是因为你使用的方法matches
要求字符串与正则表达式完全匹配,而不仅仅是它包含正则表达式。要检查遏制,您应该改用Matcher
该类。下面是一些示例代码:
import java.util.regex.Pattern;
import java.util.regex.Matcher;
class Program
{
public static void main(String[] args)
{
Pattern pattern = Pattern.compile("([a-z])\1{3}");
Matcher matcher = pattern.matcher("asdffffffasdf");
System.out.println(matcher.find());
}
}
Result:
结果:
true
回答by Michael Mrozek
You can put {n}
after something to match it n
times, so:
你可以把{n}
一些东西放在后面来匹配它的n
时间,所以:
([a-z]){3}
回答by instanceof me
General regex pattern for predefinite repetition is {4}
.
预定义重复的一般正则表达式模式是{4}
.
Thus here ([a-z])\1{3} should match your 4 chars.
因此这里 ([az])\1{3} 应该匹配你的 4 个字符。