1

how can I select date from text in java? for example if I have dates in format: 2007-01-12abcd, absc2008-01-31 and I need to have dates in format: 2007-01-12, 2008-01-31 (without text). I used matcher in my code but it is not working.

for (int i=0; i < list.size(); i++) {
    Pattern compiledPattern = Pattern.compile("((?:19|20)[0-9][0-9])-(0?[1-9]|1[012])-(0?[1-9]|[12][0-9]|3[01])",  Pattern.CASE_INSENSITIVE);
    Matcher matcher = compiledPattern.matcher(list.get(i));
    if (matcher.find() == true) {
        new_list.add(list.get(i));  
    }
}
0

2 Answers 2

2

I would keep things simple and just search on the following regex pattern:

\d{4}-\d{2}-\d{2}

It is fairly unlikely that anything which is not a date in your text already would match to this pattern.

Sample code:

String input = "2007-01-12abcd, absc2008-01-31";
String pattern = "\\d{4}-\\d{2}-\\d{2}";
Pattern r = Pattern.compile(pattern);
Matcher m = r.matcher(input);
while (m.find()) {
    System.out.println(m.group(0));
}

This prints:

2007-01-12
2008-01-31

By the way, your regex pattern can't be completely correct anyway, because it doesn't handle odd edge cases such as leap years, where February has 29 instead of 28 days.

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

2 Comments

do you know how I can add condition to not include a date in incorrect format for exemple 2009-01-111?
@ElżbietaRudzińska Don't use regex for this, instead look into using SimpleDateFormat#parse. I view extracting and validating as 2 separate tasks.
0

well i havent made a code but i think i might help you. First of all I presuppose that the format of the date in the string is already the right way(the order of the numbers is right and there are commas between the dates). Go through the string with a for-each for each character. If the current character(char) is a proper letter like a, b or c then you donw add it to the final string. If not you do add it. If the character is a comma you have to add this string to the list. The same should happen if it is the last character. This might not be the best way to do that but i am very sure it should work

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.