我试图找出以下代码打印的原因" 2"而不是" 1"
import re
line = "9111222"
m = re.match( r'.*(\w)\1+', line)
print m.group(1)
我知道re.match尝试在字符串的开头匹配,但我认为它会看到" 111"并打印" 1"
答案 0 :(得分:4)
正则表达式中的*
量词是 greedy ,这意味着它将尝试尽可能多地匹配。在您的字符串中,91112(2)2
将允许.*
匹配最多的字符,以便引擎选择匹配,并捕获倒数第二个2
。< / p>
答案 1 :(得分:2)
static void getLargest(int[] array1) {
int c;
int d = array1[0];
for (c = 0; c <= 9; c++) {
if (d < array1[c]) {
d = array1[c];
}
}
System.out.println("The highest value is: " + d);
}
表示贪婪,如果您对111感兴趣,请添加*
以使其非贪婪
?