Home > Blockchain >  Remove/match quotes between string using Regex?
Remove/match quotes between string using Regex?

Time:09-16

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 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.

CodePudding user response:

([^"] "[^"] ")([^"] )"([^"] )" 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

  • Related