简体   繁体   中英

How to extract dates from a given string in Java

For example, in:

"ABC 1 January 2014"
  "$%^DA sfad AD Friday 18 April 2014"
  "GOOD Day Tuesday 13 May 2014"

I want get the date strings from them, like

"1 January 2014"
  "18 April 2014"
  "13 May 2014"

If the prefix strings are already known, is it possible use regex to achieve it? If so how to do that?

If your strings are really that simple you can use

"\\b\\d{1,2}\\s+(January|February|March...etc)\\s+\\d{4}\\b"

Explanation:

\\b            # word boundary
\\d{1,2}       # one or two digits
\\s+           # some whitespace
(January etc)  # all months, spelled out, delimited by |
\\s+           # more whitespace
\\d{4}         # exactly 4 digits
\\b            # word boundary

You will need to use a good regular expression to extract that information. Take a look on the following links:

http://docs.oracle.com/javase/tutorial/essential/regex/

http://www.regular-expressions.info/reference.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM