0

How can i remove quotation marks around the phrase "Service Name" - "Users Connected: 359" - "Firstseen: 591230-EF" of only "Firstseen: 591230-EF", so it will become "Service Name" - "Users Connected: 359" - Firstseen: 591230-EF, using Regex?

Raul Chiarella
  • 518
  • 1
  • 8
  • 25
  • The question isn't clear, why can't you just remove all double quotes? – Barmar Sep 14 '22 at 19:13
  • Because i need to remove quotes between only a specific pattern. Other quotes must remain... Ex. "This is a long phrase" - "Part 2 of the Phrase" - "Part 3". Lets say i only want to remove quotes around "Part 3" string. – Raul Chiarella Sep 14 '22 at 19:18
  • 1
    Regular expressions are very poor at doing these types of context-dependent replacements. – Barmar Sep 14 '22 at 19:20
  • What is the logic that distinguishes `Part 3` from the other phrases that shouldn't have quotes removed. – Barmar Sep 14 '22 at 19:20
  • When doing some Analytics, multiple informations on same phrase, separated by commas and other characters, means different things so i cant change them all. Just the ones i want. I will also use this same formula for multiple patterns that uses not only quotes but other special characters as well... – Raul Chiarella Sep 14 '22 at 19:28
  • So it isnt possible using RegEx then? – Raul Chiarella Sep 14 '22 at 19:29
  • Since you haven't clearly explained the logic, it's hard to tell. Is `Part 3` a fixed phrase, or just an example of a pattern that needs to be recorgnized? – Barmar Sep 14 '22 at 19:41
  • Its a example of a pattern that needs to be regorgnized. – Raul Chiarella Sep 14 '22 at 19:47
  • OK, so what is the general pattern that it's an instance of? – Barmar Sep 14 '22 at 19:48
  • "Service Name" - "Users Connected: 359" - "Firstseen: 591230-EF". This is the exact pattern i receive, and i need to remove the quotes around "Firstseen". – Raul Chiarella Sep 14 '22 at 19:52
  • I don't need more examples, I need a general description of the pattern that should be matched, which can then be translated into a regular expression. – Barmar Sep 14 '22 at 19:54
  • "Service Name" - "Users Connected: 359" - "Firstseen: 591230-EF" becomes "Service Name" - "Users Connected: 359" - Firstseen: 591230-EF. This is it. – Raul Chiarella Sep 14 '22 at 19:55
  • Thats not a example. Thats exactly the name of a item i need to filter ._. – Raul Chiarella Sep 14 '22 at 19:56

1 Answers1

-1

Replace the string surrounded by quotes with just the string. You can use a capture group in the regexp to get the string between the quotes.

text = '"Service Name" - "Users Connected: 359" - "Firstseen: 591230-EF"'
phrase = 'Firstseen: 591230-EF'
new_text = re.sub(f'"({phrase})"', r'\1', text)
print(new_text)

Output:

"Service Name" - "Users Connected: 359" - Firstseen: 591230-EF

If you want to match something other than a fixed string, put that into phrase. For instance, you can match two different fixed strings with alternation:

phrase = 'Part 3|Data 4'

If you want to match any code after Firstseen, it would be

phrase = r'Firstseen: \d+-[A-Z]+'
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • Yeah but im not using Python... As i said, its a RegEx pattern for Grafana. – Raul Chiarella Sep 14 '22 at 19:47
  • I don't know Grafana, but I've updated the answer to use a regexp replacement. – Barmar Sep 14 '22 at 19:52
  • I've updated the answer based on your comments. Please update the question to explain what you want. – Barmar Sep 14 '22 at 19:59
  • I changed the question to match your answer. Thanks for your effort. I will make another question with the problem specified more clearly. I went into some problems using your method. For instance, if Firstseen: 591230-EF change to Firstseen: 4925-EF your ReGex stops working. – Raul Chiarella Sep 15 '22 at 12:00
  • Hello. I followed your suggestion and created a new answer containing exactly what i need. I tried stickying to the most detail as possible. Thanks for your suggestion, i went through this one again and indeed it needed more details... If you want, you can give it a try... It's a little bit different than the original. Thanks again for your suggestions and tips for making a clear and detailed question. – Raul Chiarella Sep 15 '22 at 12:47
  • Where is your answer? Do you mean you edited the question? Or you're talking about this other question: https://stackoverflow.com/questions/73731406/remove-match-quotes-between-string-using-regex – Barmar Sep 15 '22 at 15:04
  • Yes i edited to match your answer, since it lacked details. The new one is more detailed and better explained. Also, yes, it is that other question indeed. – Raul Chiarella Sep 15 '22 at 15:35
  • Your comment said you posted a new answer, you meant you posted a new question. – Barmar Sep 15 '22 at 15:36
  • Yes, new answer solved the problem. – Raul Chiarella Sep 15 '22 at 15:37