-1

I have this regexp:

^(?<FOOTER_TYPE>[ a-zA-Z0-9-]+)?(?<SEPARATOR>:)?(?<FOOTER>(?<=:)(.|[\r\n](?![\r\n]))*)?

Which I'm using to match text like:

BREAKING CHANGE: test
my multiline
string.

This is not matched

You can see the result here https://regex101.com/r/gGroPK/1

enter image description here

However, why is there the last Group 4 ?

enter image description here

LppEdd
  • 18,845
  • 6
  • 70
  • 121

2 Answers2

0

it is group 4 because the fourth parentheses you defined is: (.|[\r\n](?![\r\n]))*) it translate to

"either dot, or the following regex"

and in the example you have, it ends on a dot.

string.

so as regex is usually greedy, it captures dot as the forth group

Avishay Cohen
  • 1,632
  • 2
  • 19
  • 32
0

You will need to make last group non-capturing:

^(?<FOOTER_TYPE>[ a-zA-Z0-9-]+)?(?<SEPARATOR>:)?(?<FOOTER>(?<=:)(?:.|[\r\n](?![\r\n]))*)?

Make note of:

(?:.|[\r\n](?![\r\n]))*)?

(?: at the start makes this optional group non-capturing.

Updated Demo

anubhava
  • 713,503
  • 59
  • 514
  • 593