Search code examples
regexstringgrafanaregex-group

Remove/match quotes between string using Regex?


I have the following set of data:

Production App "Old": Service Name: ".ProdApp - Slave1"
Production App "Old": Service Name: ".ProdApp - Slave2"
Production App "Old": Service Name: ".ProdApp - Slave3"

What i want to achieve: Transform this data set to the block below, by using RegEx to match the second occurence of strings between quotes, and remove the quotes around it.

Production App "Old": Service Name: .ProdApp - Slave1
Production App "Old": Service Name: .ProdApp - Slave2
Production App "Old": Service Name: .ProdApp - Slave3

I went into this site, and tested the following patterns, based on answer here on StackOverflow:

Test string: Production App "Old": Service Name: .ProdApp - Slave1

Tests made in https://regex101.com/

Below the questions here at StackOverflow that i followed and tried its patterns:

Question 1 - How to remove matching quotes when quotes surrounds word that starts with : or # (regex)

1. /\"([#:][^\"]*)\"/ # Matched < ": Service Name: " > instead of ".ProdApp - Slave1", Created Group 1 < : Service Name: >
2. /"\s*([#:].+?)\s*"/ # Matched < ": Service Name: " > instead of ".ProdApp - Slave1", Created Group 1 < : Service Name: >

Question 2 - How can I match a quote-delimited string with a regex?

1. /".*?"/ # Matched the first occurrence < "Old" >
2. /"[^"]+"/ # Matched the first occurrence < "Old" >

Question 3 - RegEx: Grabbing values between quotation marks

1. /(["'])(?:(?=(\\?))\2.)*?\1/ # Matched the first occurence < "Old" >, created two groups Group 1: < " >, Group 2: < Null >
2. /"([^"]*)"/ # Matched < "Old" > and created Group 1 < Old >

Question 4 - Regex for quoted string with escaping quotes

1. /"(?:[^"\\]|\\.)*"/ # # Matched the first occurrence < "Old" >
2. /"([^"\\]*(\\.[^"\\]*)*)"/ # # Matched < "Old" > and created Group 1 < Old >
3. /"(?:\\"|.)*?"/ # Returned < null >
4. /(["\']).*?(?<!\\)(\\\\)*\1/ # Matched < "Old" > and < " >

The two most accurate of them was ".?" and /"([^"])"/, found on Question 2 and 3, but then it matched the first occurence only. I supose that is there a way to get the second occurence of a match using RegEx?

My favorite so far are Question 3 Solution 2 and Question 4 Solution 2, since it created Group 1 of string without quotes already... The only problem with this is that they are returning me the first occurence only. I need the second occurence which is ".ProdApp - Slave1", ".ProdApp - Slave2", ".ProdApp - Slave3", etc...

I do not know how to get second occurrence using RegEx though.

Can someone please help me? Thank you.

Edit: For knowledge, i need to apply this RegEx to a output of a query in Grafana. It is not possible to use any programming languages. Below a image of a already working query on Grafana that does a replacement:

Image 1. This one i added $ABC just so you can see that i can replace a string with another pattern.

RegEx Example

Image 2. How it actually is - It deletes the occurence, instead of replacing it.

RegEx Example 2

What it does:

It changes:

Production App "Old": Status do Serviço "\.ProdSlave*" - Service Name: ".ProdApp - Slave1"

To:

Production App "Old": Service Name: ".ProdApp - Slave1"

Next step would be replacing second occurence of quoted string to become:

Production App "Old": Service Name: .ProdApp - Slave1

So, as you can see, because of the giant name, the visualization is quite hard to do since it does not fit the dashboards panel. I need to remove the quotes for better visualization, and also for space gain. This, in my point of view, is also a good learning experience since I can think of numerous solutions using the same logic.


Solution

  • ([^"]+"[^"]+")([^"]+)"([^"]+)" substitute with $1$2$3

    https://regex101.com/r/FZncnW/1

    I'm sure there is a more clever way to write that, but I think this is pretty easy to read and maintain, which I prefer :) We are leveraging groups (or capturing groups using parenthesis).

    It is matching and capturing up to the 3rd double quote (but not including it), continuing to capture to the 4th quote (but not including that).

    Production App "Old": Service Name: ".ProdApp - Slave1"

    becomes:

    Production App "Old": Service Name: .ProdApp - Slave1