Skip to content
Advertisement

Regex: how to add a capture group?

I currently have this regex:

[(?:w+*?s+)?([^][]+)]

I want to add another capture group, so I can capture the text value before the groups.

For example:

Je naam (verplicht) [text* your-test]

My regex currently captures [text* your-test]. I want to add another group that captures the “Je naam (verplicht)” part.

How can I achieve this?

Advertisement

Answer

You may use this regex with additional capture group:

([^][h]+(?:h+[^][h]+)*)?h+[(?:w+*?h+)?([^][]+)]

Updated RegEx Demo

  • ([^][h]+(?:h+[^][h]+)*)? is new optional capture group that matches and captures text before [...] into a separate group.
  • [^][h]+ matches 1 or more of any character that is not [, ] and whitespace.
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement