I found a code in Java Regular expression which is confusing to me:
Pattern.compile( "J.*\\d[0-35-9]-\\d\\d-\\d\\d" );
The string to be compiled is:
String string1 = "Jane's Birthday is 05-12-75\n" + "Dave's Birthday is 11-04-68\n" + "John's Birthday is 04-28-73\n" + "Joe's Birthday is 12-17-77";
What does it mean by the
[0-35-9]
And why there are 4 "\d"s instead of 3? I assume there are only 3 numbers in the birthday.
The form of \\d simply matches a digit, not a number.
So using the pattern of \\d\\d will match two consecutive digits.
Using \\d\\d-\\d\\d will match two consecutive digits, a - literally, two consecutive digits.
Let's take a look at your match and why.
Joe's Birthday is 12-17-77
^ match a digit 0 to 9
^ match any character of '0' to '3', '5' to '9'
^ match a '-' literally
^ match a digit 0 to 9
^ match a digit 0 to 9
^ match a '-' literally
^ match a digit 0 to 9
^ match a digit 0 to 9
The [0-35-9] part matches any character of 0 to 3, 5 to 9
Your whole regular expresson explained:
J 'J'
.* any character except \n (0 or more times)
\d match a digit 0 to 9
[0-35-9] any character of: '0' to '3', '5' to '9'
- match a '-' literally
\d match a digit 0 to 9
\d match a digit 0 to 9
- match a '-' literally
\d match a digit 0 to 9
\d match a digit 0 to 9
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With