-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
  • 20,274
  • 11
  • 84
  • 139

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,978
  • 2
  • 21
  • 34
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
  • 761,203
  • 64
  • 569
  • 643