3

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.

0

3 Answers 3

2

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
Sign up to request clarification or add additional context in comments.

3 Comments

Hi one simple question. Say the date is surrounded by a couple of strings before and after and I want to match the regex pattern between those particular strings, would this work [^string1 string2 (0?[1-9]|[12][0-9]|3[01])([- /.])(0?[1-9]|1[012])\\2(19|20)\\d\\d string3 string4$]
@newtoprogramming yes it should (provided those square brackets are not part of your pattern). why don't you just try it?
this is the exact line for which I am trying to write a regex "commented on 7/2/2013 (version1.0)" for which, I tried "commented on (?:0?[1-9]|[12][0-9]|3[01])([- /.])(?:0?[1-9]|1[012])\\1(?:19|20)?\\d\\d (version 1.0)" and also "(?<=commented\\son\\s(?:0?[1-9]|[12][0-9]|3[01])([- /.])(?:0?[1-9]|1[012])\\1(?:19|20)?\\d\\d?=\\s(version 1.0))" neither detects my pattern. Can you help me. I do not know what I am doing wrong.
1

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$");

1 Comment

Are you sure changing the input is a valid solution? ^^ I think this is about matching the given input and not finding an input matched by the pattern.
1

regex you used is not correct. Task section to match day for example

0[1-9]|[12][0-9]|3[01])

This regex means 0 should be used as prefix if day is 1~9.

To fix this issue , you should add ? which means preceding item is optional. So you can change your regex string to

(0?[1-9]|[12][0-9]|3[01])[- /.](0?[1-9]|1[012])[- /.](19|20)\d\d

In future, you can use regular expression tester to debug those issues. They are useful and helps to save time. For example, http://regex.uttool.com , http://regexpal.com/

2 Comments

For Java problems, the OP should rather use regexplanet.com. I don't know about regex.uttool.com, but regexpal uses JavaScript's regex flavor.
Both of these two are using javascript regex flavor.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.