Help, what's the problem?

Pattern patternDate = Pattern.compile("(?<=Опубликован:)(.*)(?=\\|)"); Matcher matcherDate = patternDate.matcher("04.03.2017 | 20:13 [последние изменения: 04.03.2017 | 20:14]"); while (matcherDate.find()) { System.out.println(matcherDate.group()); } 

I am getting 03/04/2017 | 20:13 [last modified: 03/04/2017

I need 04.03.2017

  • Use the exciting group and exclusive character class - "Опубликован:\\s*([^|]+)" -> if (matcherDate.find()) { System.out.println(matcherDate.group(1));} - Wiktor Stribiżew

1 answer 1

Use not greedy comparison. To do this after * put ?

 Pattern patternDate = Pattern.compile("(?<=Опубликован:)(.*?)(?=\\|)")