Reputation: 23
I have regex=""
and a String str="stackoveflow";
I don't understand why it is matching every character in the string. can you explain to me?
public class test {
public static void main(String[] args){
Console console = System.console();
String str="stackoveflow";
Pattern pattern = Pattern.compile("");
Matcher matcher = pattern.matcher(str);
while (matcher.find()) {
console.format("I found the text" +
" \"%s\" starting at " +
"index %d and ending at index %d.%n",
matcher.group(),
matcher.start(),
matcher.end());
}
}
}
Output is:
I found the text "" starting at index 0 and ending at index 0. I found the text "" starting at index 1 and ending at index 1. I found the text "" starting at index 2 and ending at index 2. I found the text "" starting at index 3 and ending at index 3. I found the text "" starting at index 4 and ending at index 4. I found the text "" starting at index 5 and ending at index 5. I found the text "" starting at index 6 and ending at index 6. I found the text "" starting at index 7 and ending at index 7. I found the text "" starting at index 8 and ending at index 8. I found the text "" starting at index 9 and ending at index 9. I found the text "" starting at index 10 and ending at index 10. I found the text "" starting at index 11 and ending at index 11. I found the text "" starting at index 12 and ending at index 12.
Upvotes: 1
Views: 556
Reputation: 718788
Pattern("")
matches a string consisting of zero characters. You can find one of those at every position in the string.
Note: if you changed find
to match
, you should find that there are no matches. (With match
the pattern needs to match the entire input, and the entire input does not match a sequence of zero characters.)
Before you edited the question, your pattern was Pattern("e*")
. That means zero or more repetitions of the character 'e'
. By the logic above, you can "find" one of those at every character position in the input.
Upvotes: 2