0

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)

2 Answers 2

7
\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

1
  • Excellent. To make it even safer, one could add \b at the beginning and the end.
    – Ingo
    Apr 6, 2011 at 11:44
1

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

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.