所以我是相当新的Regexp…到目前为止,我一直在使用Regexp + loop:
boolean match = false; int number =0;
int number =0;
String Str1 = String.valueOf(451999277);
for (int i=0;match1 == false;i++) {
//check the pattern through loop
match1 = Pattern.matches(".*" + i + i + i + ".*", Str1);
number = i;// assigning the number (i) which is the triplet(occur 3 times in a row) in the givin int
}
我的目标是在给定的整数中找到一个是三元组的数字。例如:
我想从451999277中提取:"9";"9"出现3次,即"999"
,但我很确定必须有一个解决方案,只使用Regexp....如果有人能帮我找到解决方案,那就太好了......提前感谢
使用捕获组匹配数字,然后稍后引用它:
(d)11
将匹配一个数字,将其捕获在一个组中(在本例中为数字1,因为它是正则表达式的第一组),然后立即匹配第1组中的任何内容两次。
Pattern regex = Pattern.compile("(\d)\1\1");
Matcher regexMatcher = regex.matcher(subject);
if (regexMatcher.find()) {
ResultString = regexMatcher.group();
}
将查找subject
中的第一个匹配项(如果有的话)。