2

I'm making a date extractor using regex in java. Problem is that date is 20-05-2014 and my program is extracting 0-5-14. In short, how can I get the character on which I'm checking the second character of date?

    int count = 0;
    String data = "HellowRoldsThisis20-05-2014. [email protected]@gmail.com";
    String regexOfDate = "((?<=[0])[1-9]{2})|((?<=[12])[0-9])|((?<=[3])[01])\\.\\-\\_((?<=[0])[1-9])|((?<=[1])[0-2])\\.\\-\\_((?<=[2])[0-9]{4})"; \\THE PROBLEM
    String[] extractedDate = new String[1000];
    Pattern patternDate = Pattern.compile(regexOfDate);
    Matcher matcherDate = patternDate.matcher(data);

    while(matcherDate.find()){           
        System.out.println("Date "+count+"Start: "+matcherDate.start());
        System.out.println("Date "+count+"End  : "+matcherDate.end());
        extractedDate[count] = data.substring(matcherDate.start(), matcherDate.end());
        System.out.println("Date Extracted: "+extractedDate[count]);
    }

2 Answers 2

2

You can try the regular expression:

// (0[1-9]|[12][0-9]|[3][01])[._-](0[1-9]|1[0-2])[._-](2[0-9]{3})
"(0[1-9]|[12][0-9]|[3][01])[._-](0[1-9]|1[0-2])[._-](2[0-9]{3})"

Debuggex

Sign up to request clarification or add additional context in comments.

1 Comment

How can one draw regex?
0

A single regex o match valid dates is awful.

I'd do:

String regexOfDate = "(?<!\\d)\\d{2}[-_.]\\d{2}[-_.]\\d{4}(?!\\d)";

to extract the potential date, then test if it is valid.

Comments

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.