Add match to the same group when using pipe

Viewed 29

How to make both matches belong to group 1?

regex:

^.{7}(.*).{4}|(.*).{4}

values:

QOQSNT.ini
QOQSNT_MSSQLSERVER.ini

Result:

enter image description here

Red is Group 1

Green is Group 2

1 Answers

In PCRE and Boost, you can use a branch reset group:

(?|^.{7}(.*).{4}|(.*).{4})
^^^             ^        ^

See the regex demo.

However, you may also use

([^_]+)\.[^.]+$

See the regex demo.

Details

  • ([^_]+) - Group 1: any one or more chars other than _
  • \. - a dot
  • [^.]+ - 1+ chars other than a dot
  • $ - end of string.
Related