You may remove a (...)
substring with a leading whitespace at the end of the string only:
\s*\([^()]*\)$
See the regex demo.
Details
\s*
- 0+ whitespace chars
\(
- a (
[^()]*
- 0+ chars other than (
and )
\)
- a )
$
- end of string.
See the Python demo:
import re
bracketedString = '*AWL* (GREATER) MINDS LIMITED (CLOSED)'
nonBracketedString = re.sub(r"\s*\([^()]*\)$", '', bracketedString)
print(nonBracketedString) # => *AWL* (GREATER) MINDS LIMITED
With PyPi regex module you may also remove nested parentheses at the end of the string:
import regex
s = "*AWL* (GREATER) MINDS LIMITED (CLOSED(Jan))" # => *AWL* (GREATER) MINDS LIMITED
res = regex.sub(r'\s*(\((?>[^()]+|(?1))*\))$', '', s)
print(res)
See the Python demo.
Details
\s*
- 0+ whitespaces
(\((?>[^()]+|(?1))*\))
- Group 1:
\(
- a (
(?>[^()]+|(?1))*
- zero or more repetitions of 1+ chars other than (
and )
or the whole Group 1 pattern
\)
- a )
$
- end of string.