0

I have a question about reluctant fetch strategy on regular expressions.

Given the following Java code:

Pattern datePattern = Pattern.compile("(.*?)(\\d\\d\\d\\d-\\d\\d-\\d\\d)(.*)");
Matcher matcher = datePattern.matcher("2017-03-16");
if(matcher.find()){
   System.out.println("Matched");
   String extractedDate = matcher.group(1);
   System.out.println("Extracted date: " + extractedDate);
}

I get this output:

Matched
Extracted date: 

So matcher.group(1) just extracts an empty string. It seems I don't understand how reluctant fetch strategy is really working. I had thought that the first defined group in the pattern:

(.*?)

will try to match as few characters as possible. In other words, when it can match something to the second group:

(\d\d\d\d-\d\d-\d\d)

then it will match it to that group and consume the first group with "nothing".

The third group should also have no effect in my opinion.

Can someone explain me why in the given example I don't get the expected string "2017-03-16" from matcher.group(1)?

Thank you

user3237736
  • 845
  • 1
  • 9
  • 24
  • 1
    Reluctant patterns are not tried before all the subsequent ones, and only if the latter fail to match, the reluctantly quantified pattern is "expanded", matches 1 char at a time. As expected, [the date is in Group 2](https://regex101.com/r/d604Ql/1). – Wiktor Stribiżew Mar 17 '17 at 21:22
  • Thank you, but does that mean that I have to basically check both group 2 and 1 which one contains my date? Or how would I have to change the pattern that I know for sure in which group the result will always be? I mean the pattern should also work for "something-2017-03-16" – user3237736 Mar 17 '17 at 21:27
  • 2
    Just use [`"\\d{4}-\\d{2}-\\d{2}"`](http://ideone.com/tCC8Ns) - the match will always be in `.group()`. – Wiktor Stribiżew Mar 17 '17 at 21:35
  • Ah, I didn't know that you can group just like that. Thank you! – user3237736 Mar 17 '17 at 22:25

0 Answers0