I'm looking for a regex to find named capturing groups in (other) regex strings.
Example: I want to find (?P<country>m((a|b).+)n)
, (?P<city>.+)
and (?P<street>(5|6)\. .+)
in the following regex:
/(?P<country>m((a|b).+)n)/(?P<city>.+)/(?P<street>(5|6)\. .+)
I tried the following regex to find the named capturing groups:
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 + `)` +
`\)`)
?U
makes greedy quantifiers(+
and *
) non-greedy, and non-greedy quantifiers (*?
) greedy. Details in the Go regex documentation.
But it doesn't work because parenthesis are not matched correctly.