Some Name
Some Name

Reputation: 571

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?

Upvotes: 1

Views: 331

Answers (1)

anubhava
anubhava

Reputation: 784998

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.

Upvotes: 3

Related Questions