Hello people thanks for the help so far. I Have a regex for finding a date in a given string and it does not seem to working. Could someone tell me what I am doing wrong? The code goes something like
Pattern pattern = Pattern.compile("^(0[1-9]|[12][0-9]|3[01])[- /.](0[1-9]|1[012])[- /.](19|20)\\d\\d(?:,)$");
Matcher match = pattern.matcher(text1);
List<String> removable1 = new ArrayList<String>();
while(match.find()) {
removable1.add(match.group());
}
I know that there is a date "7/2/2013" contained in the string text1 which is not being written to the list removable1. Any help is appreciated thanks in advance.
Your pattern does not allow for single-digit days and months. Make the leading 0 optional:
^(0?[1-9]|[12][0-9]|3[01])[- /.](0?[1-9]|1[012])[- /.](19|20)\\d\\d(?:,)$
Also, I'm not sure what the trailing comma does. And additionally, you say "contained in the string", but ^ and $ anchor the pattern to the start and end. So you might want to remove these, if you want to find dates:
(0?[1-9]|[12][0-9]|3[01])[- /.](0?[1-9]|1[012])[- /.](19|20)\\d\\d
Finally, if you want to make sure that both date separators are the same character, you can do this:
(0?[1-9]|[12][0-9]|3[01])([- /.])(0?[1-9]|1[012])\\2(19|20)\\d\\d
Finally, for some optimization, avoid capturing where you don't need it (and potentially make the century optional:
(?:0?[1-9]|[12][0-9]|3[01])([- /.])(?:0?[1-9]|1[012])\\1(?:19|20)?\\d\\d
Try this
String text1="07/02/2013";
Pattern pattern = Pattern.compile("^(0[1-9]|[12][0-9]|3[01])[- /.](0[1-9]|1[012])[- /.](19|20)\\d\\d$");
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