-2
 lineIdentifier:dasda creationDate:[08/06/2018 00:00:00 TO *] text: [dasda, lll]

With regex I would like to get back only data in creationDate

08/06/2018 00:00:00 and *

This is my pattern but does not work

Pattern r = Pattern.compile("\\[(\\w*)\\]");
        Matcher m = r.matcher(result);

        if (m.find( )) {
            System.out.println("Found value: " + m.group(0) );
        }else {
            System.out.println("NO MATCH");
        }

any idea what i am doing worng?

Chris
  • 409
  • 3
  • 17
mbrc
  • 3,523
  • 13
  • 40
  • 64

1 Answers1

2

You can try:

Pattern r = Pattern.compile(".*(creationDate:\\[(.+?) TO (.+?)\\]?).*");
Matcher m = r.matcher("lineIdentifier:dasda creationDate:[08/06/2018 00:00:00 TO *] text: [dasda, lll]");

if (m.find( )) {
      System.out.println("Found value: " + m.group(2) );
      System.out.println("Found value: " + m.group(3) );
}else {
      System.out.println("NO MATCH");
}
Duong Anh
  • 529
  • 1
  • 4
  • 21
  • @Lino Why? OP doesn't want to check if regex *matches* entire text, but to *find* specific part of it. Improvement of this answer wouldn't be using `matches()` but removing unnecessary `.*` at start and end of regex. – Pshemo Jun 11 '18 at 08:28
  • @Pshemo you're right, either use `matches` with `.*` or use `find` without `.*` – Lino Jun 11 '18 at 08:33