1

I have a String variable like that "/etc/opt/eset/esets/license/esets_abd9c6.lic: ESET File Security, 2019-07-29 00:00:00, Mynet" and I need to capture the date value from that field, but I couldn't figure it out.

String string = "/etc/opt/eset/esets/license/esets_abd9c6.lic: ESET File Security, 2019-07-29 00:00:00, Mynet";
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Pattern p = Pattern.compile("");
Matcher m = p.matcher(input);
Date tempDate = null;
if(m.find())
{
    tempDate = format.parse(m.group());
}
System.out.println("" + tempDate);

Is there any regex pattern for capturing a date value from a String vaiable?

Adam Jungen
  • 307
  • 2
  • 5
  • 19

1 Answers1

2

For a date string, you can use:

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

See a demo on regex101.com and note that you need two backslashes in Java (hence ...\\b\\d{4}...)


This is
\b     # word boundary
\d{4}- # four digits and a "-"
\d{2}-
\d{2}
\b
Jan
  • 38,539
  • 8
  • 41
  • 69