我正在寻找一个正则表达式来找到(其他)正则表达式字符串中的命名捕获组.
示例:我想查找(?P<country>m((a|b).+)n),(?P<city>.+)并(?P<street>(5|6)\. .+)在以下正则表达式中:
/(?P<country>m((a|b).+)n)/(?P<city>.+)/(?P<street>(5|6)\. .+)
Run Code Online (Sandbox Code Playgroud)
我尝试了以下正则表达式来查找命名的捕获组:
var subGroups string = `(\(.+\))*?`
var prefixedSubGroups string = `.+` + subGroups
var postfixedSubGroups string = subGroups + `.+`
var surroundedSubGroups string = `.+` + subGroups + `.+`
var capturingGroupNameRegex *regexp.RichRegexp = regexp.MustCompile(
`(?U)` +
`\(\?P<.+>` +
`(` + prefixedSubGroups + `|` + postfixedSubGroups + `|` + surroundedSubGroups + `)` +
`\)`)
Run Code Online (Sandbox Code Playgroud)
?U使贪婪量词(+和*)非贪婪,非贪婪量词(*?)贪婪.Go正则表达式文档中的详细信息.
但它不起作用,因为括号不正确匹配.