Koerr
Koerr

Reputation: 15723

how can I get the date from String using regex in java?

here's my code:

String t1="postby <span title=\"2011-4-5 17:22\">yesterday&nbsp;17:22</span>";
String t2="postby 2010-11-12 10:02";

I want get 2011-4-5 17:22 , 2010-11-12 10:02 from t1 or t2,using one regex expression

(input t1 or t2,output the date)

how to do? (please give to me some example code,thanks)

Upvotes: 0

Views: 253

Answers (2)

Raedwald
Raedwald

Reputation: 48644

How many false matches will you allow? Bozho already suggested the pattern

\d{4}-\d{1,2}-\d{1,2} \d{2}:\d{2}

But that matches the following questionable cases: 0000-1-1 00:00 (there is no year zero), 2011-0-1 00:00 (there is no month zero), 2011-13-1 00:00 (there is no month 13), 2011-1-32 00:00 (there is no month-day 32) 2011-12-31 24:00 (there is at most one leap second) and 2011-12-31 23:61 (there is at most one leap seond).

You are wanting to parse date-times that are almost, but not quite, in ISO-8601 format. If you can, please use that international standard format.

In one of my programs (a shell script using grep), I've used the following regular expression:

^20[0-9][0-9]-[01][0-9]-[0-3][0-9]T[0-9][0-9]:[0-9][0-9]:[0-9][0-9]UTC$

I had an extra T and UTC to deal with, was interested only in dates in this century, and parsed with seconds precision. I see I was not so restrictive on hour and minute values, probably because traditional C/C++ conversions can handle them.

I guess you therefore could use something like the following:

\d{4}-[01]\d-[0-3]\d [0-2]\d:[0-6]\d

Upvotes: 1

Bozho
Bozho

Reputation: 597076

\d{4}-\d{1,2}-\d{1,2} \d{2}:\d{2}

A few notes:

  • you will have to escape the slashes in a string: String pattern = "\\d{4}-\\d{1,2}....."
  • \d means "digit" (0-9)
  • {x} means "x times"
  • {x,y} means "at least x, but not more than y times"

Reference: java.util.regex.Pattern

Upvotes: 7

Related Questions