Set a regex pattern to condition placing or removing spaces before or after a ) according to the characters that are before or after

Question:

import re

input_text = "((NOUN) )   ) de el auto rojizo, algo) ) )n Luego ((PL_ADVB)dentro ((NOUN)de baúl ))abajo.) )."

input_text = input_text.replace(" )", ") ")

print(repr(input_text))

Simply using the .replace(" )", ") ") function I get this bad output, as it doesn’t consider the conditional replacements that a function using regex patterns could, for example using re.sub( , ,input_text, flags = re.IGNORECASE)

'((NOUN))   )  de el auto rojizo, algo)) ) n Luego ((PL_ADVB)dentro ((NOUN)de baúl) )abajo.)) .'

The goal is to get this output where closing parentheses are stripped of leading whitespace’s and a single whitespace is added after as long as the closing parenthesis ) is not in front of a dot . , a newline n or the end of line $

'((NOUN))) de el auto rojizo, algo)))n Luego ((PL_ADVB)dentro ((NOUN)de baúl))abajo.)).'
Asked By: Matt095

||

Answers:

Try this pattern it should solve it

/(s*)())(s*)(?=[^s])/g

This pattern will match a ‘)’ that is followed by a non-whitespace character and remove any spaces before or after the ‘)’.

If you want to add spaces around a ‘)’ instead of removing them, you can modify the pattern like this:

/(s*)())(s*)(?=[^s])/g

Answered By: AceJoker Studio

Just strip the preceding whitespaces before ) with the following simple regex:

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

'((NOUN))) de el auto rojizo, algo)))n Luego ((PL_ADVB)dentro ((NOUN)de baúl))abajo.)).'
Answered By: RomanPerekhrest
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.