0

I don't have a code that needs solving, but merely a simple explanation if possible from an example in a book. There isn't much explaining and I just need to know how this line of code checks to make sure the date entered does not occur in April. The [0-35-9] is throwing me off.

 Pattern expression = Pattern.compile("J.*\\d[0-35-9]-\\d\\d-\\d\\d");
4
Contributors
7
Replies
30
Views
3 Years
Discussion Span
Last Post by brittney_2
0

One tricky part of the regex in the example is at .* part. If I remember correctly, by default regex matching will use greedy algorithm. In other words, it will keep looking for the pattern as far as it could after .* to match, not the first match it found.

// i.e.
String a = "Jane BD is 10-10-78";
String b = "Jane or Jone BD is 10-10-78 or 09-09-88";
/*
Both string should be matched but the second string would match at 09-09-88 instead of
what you would expect it to match (at 10-10-78).
*/
0

I just need to know how this line of code checks to make sure the date entered does not occur in April.

it clearly shows [0-35-9] refers to any character that falls within 0 to 3 or 5 to 9 except 4 which is april

Edited by screenedcreamy: close quotes

0

Stultuske: Your are right. I guess it should have been [1-35-9] if it is required to skip just april.

Edited by screenedcreamy: typo

0
Stultuske: Your are right. I guess it should have been [1-35-9] if it is required to skip just april.

Actually if you look at the whole regex, it is correct to be [0-35-9] in the sense that it is the second digit of a month. However, the regex is flaw by itself (too simple) because it uses \d in place of where numbers may or may not be what they are (\d represents any digit of [0-9]).

Edited by Taywin

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.