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.)).'
CodePudding user response:
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
CodePudding user response:
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.)).'