I have a sample string that I want to match: "a123456.java,a12344*javaaaaaaaaaaaaa"
I use the following regex pattern: Pattern p=Pattern.compile("a[0-9]+[.]?[a-zA-Z]+");
Now the ? operator means 0 or more occurrences of ".". Why is the string "a12344*javaaaaaaaaaaaaa" not picked up by this? Why is the * character not counted as a 0 occurrence?
If you mean that you expected the * to be picked up by the
.because that means ‘anything’:Inside the character class, the
.becomes a literal., instead of the character meaning ‘anything’.If you want to match anything, then use
.?instead of[.]?If you meant that
*is not., so is zero occurrences of.:You are right, but in your regex, the
.must be followed by a letter ([a-zA-Z]), and the*is obviously not a letter.To clarify, you have: