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

Question:

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)"
Asked By: Matt095

||

Answers:

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)'
Answered By: RomanPerekhrest

Try the following. See regex

re.sub(r")[A-Za-z]", ")", input_text)
Answered By: John Williams

You could use a single pattern with a lambda, and do the replacement by checking if the group 1 value exists.

Explanation

  • s*) Match optional whitespace chars and )
  • (?:s{2,})? Optionally match 2 or more whitespace chars
  • ([^W)])? Optional capture group 2, match a non word character other than )

See the regex101 matches and a Python demo.

import re

pattern = r"s*)(?:s{2,})?([^W)])?"
input_text = "((PL_ADVB)dentro ). ((PL_ADVB)ñu)((PL_ADVB)    9u)"
res = re.sub(pattern, lambda m: ") " + m.group(1) if m.group(1) else ")", input_text)
print(res)

Output

((PL_ADVB) dentro). ((PL_ADVB) ñu)((PL_ADVB) 9u)
Answered By: The fourth bird