我想知道如何使用正则表达式从 python 中的字符串中删除以下文本。
string = "Hello (John)"
(magic regex)
string = "Hello "
但是,如果括号中的文本包含子字符串“John”,我只想删除它。例如,
string = "Hello (Sally)"
(magic regex)
string = "Hello (Sally)"
这可能吗?谢谢!
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)
这应该是您想要的要点:
>>> 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
如果您只删除 John 的所有实例,您可以执行以下操作:
string = "Hello (John)"
string.replace("(John)", "")
print(string) # Prints "Hello "