简体   繁体   English

正则表达式:如何添加捕获组?

[英]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] .我的正则表达式当前捕获[text* your-test] I want to add another group that captures the " Je naam (verplicht) " part.我想添加另一个捕获“ Je naam (verplicht) ”部分的组。

How can I achieve this?我怎样才能做到这一点?

You may use this regex with additional capture group:您可以将此正则表达式与其他捕获组一起使用:

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

Updated RegEx Demo更新的 RegEx 演示

  • ([^][\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. [^][\h]+匹配任何不是[ , ]和空格的字符中的 1 个或多个。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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