简体   繁体   中英

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM