Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

In Python, find all occurrences of a group of characters in a string and remove the last character from each occurence

I have a large string and I would like to find all occurrences of one or more consecutive right curly brackets (for any number of curly brackets) that are surrounded by double quotes, and remove the last double quote.

I thought I could do this with a regex and "re", and so far I have the following, however I’m not sure what to replace "???" with. I’m not even sure if re with a regex is the correct way to go in the first place:

import re
my_string= r'abc"}"abc"}}"abc"}}}"abc"}}}}"abc"{abc}"'
result = re.sub(r'"}+"', ???, my_string)
print(result)

… my desired result is this:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

abc"}abc"}}abc"}}}abc"}}}}abc"{abc}"

How can I achieve this in Python? Thank you!

>Solution :

You could use a capture group to keep what is before the closing double quote.

("}+)"

And replace with capture group 1.

Regex demo

Example

import re

my_string= r'abc"}"abc"}}"abc"}}}"abc"}}}}"abc"{abc}"'
result = re.sub(r'("}+)"', r"\1", my_string)
print(result)

Output

abc"}abc"}}abc"}}}abc"}}}}abc"{abc}"
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading