consider these two examples:
testFind("\\W.*", "@ this is a sentence");
testFind(".*", "@ this is a sentence");
Here's my testFind method
private static void testFind(String regex, String input) {
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(input);
int matches = 0;
int nonZeroLengthMatches = 0;
while (matcher.find()) {
matches++;
String matchedValue = matcher.group();
if (matchedValue.length() > 0) {
nonZeroLengthMatches++;
}
System.out.printf("Matched startIndex= %s, endIndex= %s, value: '%s'\n",
matcher.start(), matcher.end(), matchedValue);
}
System.out.printf("Total non zero length matches = %s/%s \n", nonZeroLengthMatches, matches);
}
Here's the output:
---------------------
Regex: '\W.*', Input: '@ this is a sentence'
Matched startIndex= 0, endIndex= 20, value: '@ this is a sentence'
Total non zero length matches = 1/1
---------------------
Regex: '.*', Input: '@ this is a sentence'
Matched startIndex= 0, endIndex= 20, value: '@ this is a sentence'
Matched startIndex= 20, endIndex= 20, value: ''
Total non zero length matches = 1/2
According to this: https://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html
Greedy quantifiers ..... X* X, zero or more times
My question is why in case of regex = "\\W.*" matcher is not giving zero-length match?
因为"\\W.*"
意思是: "\\W"
-一个非单词字符,加上".*"
-零次或多次的任何字符,因此只有'@...'
于此模式"\\W.*"
,但""
不匹配。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.