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

Replace ")" by ") " if the parenthesis is followed by a letter or a number using regex

import re

input_text = "((PL_ADVB)dentro ). ((PL_ADVB)ñu)((PL_ADVB)    9u)"

input_text = re.sub(r"\s*\)", ")", input_text)
print(repr(input_text))

How do I make if the closing parenthesis ) is in front of a letter (uppercase or lowercase) it converts the ) to ), so that the following output can be obtained using this code…

"((PL_ADVB) dentro). ((PL_ADVB) ñu)((PL_ADVB) 9u)"

>Solution :

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

Perform consecutive substitutions (including stripping extra spaces through all the string):

input_text = "((PL_ADVB)dentro ). ((PL_ADVB)ñu)((PL_ADVB)    9u)"

input_text = re.sub(r"\s*\)", ")", input_text)
input_text = re.sub(r"\s{2,}", " ", input_text)  # strip extra spaces
input_text = re.sub(r"\)(?=[^_\W])", ") ", input_text)
print(repr(input_text))

'((PL_ADVB) dentro). ((PL_ADVB) ñu)((PL_ADVB) 9u)'
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