正则表达式有条件地删除parens中的文本

时间:2013-10-20 19:40:31

标签: python regex

我想知道如何使用正则表达式从python中的字符串中删除以下文本。

string = "Hello (John)"
(magic regex)
string = "Hello "

但是,如果它包含子字符串“John”,我只想删除parens中的文本。例如,

string = "Hello (Sally)"
(magic regex)
string = "Hello (Sally)"

这可能吗?谢谢!

3 个答案:

答案 0 :(得分:1)

这应该是你想要的要点:

>>> from re import sub
>>> mystr = "Hello (John)"
>>> sub("(?s)\(.*?John.*?\)", "", mystr)
'Hello '
>>> mystr = "Hello (Sally)"
>>> sub("(?s)\(.*?John.*?\)", "", mystr)
'Hello (Sally)'
>>> mystr = "Hello (John) My John (Sally)"
>>> sub("(?s)\(.*?John.*?\)", "", mystr)
'Hello  My John (Sally)'
>>>

故障:

(?s)   # Dot-all flag to have . match newline characters
\(     # Opening parenthesis
.*?    # Zero or more characters matching non-greedily
John   # Target
.*?    # Zero or more characters matching non-greedily
\)     # Closing parenthesis

答案 1 :(得分:1)

如果你要删除John的所有实例,你可以这样做:

string = "Hello (John)"
string.replace("(John)", "")
print(string) # Prints "Hello "

答案 2 :(得分:1)

import re
REGEX = re.compile(r'\(([^)]+)\)')

def replace(match):
    if 'John' in match.groups()[0]:
        return ''
    return '(' + match.groups()[0] + ')'

my_string = 'Hello (John)'
print REGEX.sub(replace, my_string)
my_string = 'Hello (test John string)'
print REGEX.sub(replace, my_string)
my_string = 'Hello (Sally)'
print REGEX.sub(replace, my_string)

Hello 
Hello 
Hello (Sally)