Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex Extract a capture group based on a parent capture group

Tags:

regex

grafana

I'm trying to format a drop down variable in Grafana in which the values are structured email addresses that I need to set a portion to the named capture group and place the entire email address in the named capture group . The general details of what I'm trying to do is explained on the Grafana Support page. What I'm looking to do is take the values in a variable query and modify them in the regex area to accomplish the following.

With the query result values of:

[email protected]
[email protected]
[email protected]

I would like the regex to produce two named capture groups text and value.

<text>    |     <value>
info      |     [email protected]
errors    |     [email protected]
warnings  |     [email protected]

The regex (\S+\+)?(?<text>\S+)@.*|(?<value>.*) only seems to set the <text> capture group.

Is this something that is possible?

like image 970
Aaron Avatar asked Nov 18 '25 17:11

Aaron


1 Answers

You can use

(?<value>[^@\s]+\+(?<text>[^@\s+]*)@.*)

See the regex demo.

Details:

  • (?<value> - Group "value" start:
    • [^@\s]+ - one or more chars other than @ and whitespace
    • \+ - a literal + char (needs escaping, or it would be a quantifier)
    • (?<text> - Group "text" start:
      • [^@\s+]* - zero or more chars other than @, whitespace and +
    • ) - end of Group "text"
  • @ - a @ char
  • .* - any zero or more chars other than line break chars as many as possible
  • ) - end of Group "value".
like image 165
Wiktor Stribiżew Avatar answered Nov 21 '25 09:11

Wiktor Stribiżew



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!