Home > front end >  Retrieving the 12th through 14th characters from a long strong using ONLY regex - Grafana variable
Retrieving the 12th through 14th characters from a long strong using ONLY regex - Grafana variable

Time:10-06

I have a small issue, I am trying to get specific characters from a long string using regex but I am having trouble.

Workflow

Prometheus --> Grafana --> Variable (using regex)

I can't use anything other than Regex expressions to achieve this result

I am currently using this expression to grab the long string from some json output:

.*channel_id="(.*?)".*

FROM THIS

{account_id="XXXXXXX-xxxx-xxxx-xxxx-xxxxxxxxxx",account_name="testalpha",channel_id="s0022110430col0901241usa",channel_abbr="s0022109430col} 

This returns a string that's ALWAYS 24 characters long:

s0022110430col0901241usa

PROBLEM:

I need to grab the 3 letters 'col' and 'usa' as they are the two teams that are playing, ideally I would be able to pipe the results from the first regex to get these values (the position is key, since the first value will ALWAYS be the 12-14th characters and the second value is the last 3 characters) if I could output these values in uppercase with the string "vs" in between to create a string such as:

COL vs USA

or

ARG vs BRA

I am open to any and every suggestion anyone may have

Thank you!

PS - The uppercase thing is 'nice to have' BUT not needed

CodePudding user response:

I'm still learning RegEx, so this is all I could come up with:

For the col (first team):

(?<=(channel_id=".{11}))\w{3}

For the usa (second team):

(?<=(channel_id=".{21}))\w{3}

CodePudding user response:

Can you define the channel_id?

It begins with 's' and then there are many numbers. If they are always numbers, you can use this regex:

channel_id=".[0-9]*([a-z]*)[0-9]*([a-z]*)

You will get 2 groups, one with "col" and the other with "usa".

Edit: Or if you just know, that you have always the same size, you can use something like:

channel_id=".{11}([a-z]*).{7}([a-z]*)
  • Related