0

This regexp finds the four groups as expected:

https://regex101.com/r/5dF794/1

Since sometimes the second group is missing, I added the ? to make it optional, but it never finds the second group:

https://regex101.com/r/0Jbcwy/1

Why is the second group never captured, even when it does exist?

Here are the two patterns and the test text:

^(.).* - (?:(\d+)mm - ).*\(([\d\.]+)\" x ([\d\.]+)\"\)
^(.).* - (?:(\d+)mm - )?.*\(([\d\.]+)\" x ([\d\.]+)\"\)
N251 - 8mm - 4' x 10' (49.21" x 122.05")
stenci
  • 8,290
  • 14
  • 64
  • 104
  • 2
    See https://regex101.com/r/0Jbcwy/2 for lazy quantifier usage with your regex. – ctwheels Feb 26 '18 at 19:21
  • 1
    Yeah, the `.*` after your first capture group is greedy, and since the group after it is optional, the `.*` includes that stuff. That's why `.*?` fixes it. – zzxyz Feb 26 '18 at 19:28

0 Answers0