Python Regex有条件地删除parens中的文本

Python Regex有条件地删除parens中的文本,python,regex,Python,Regex,我想知道如何使用正则表达式从python中的字符串中删除以下文本 string = "Hello (John)" (magic regex) string = "Hello " 但是,我只想删除parens中包含子字符串“John”的文本。那么比如说, string = "Hello (Sally)" (magic regex) string = "Hello (Sally)" 这可能吗?谢谢 这应该是你想要的要点: >>> from re import sub >&

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

string = "Hello (John)"
(magic regex)
string = "Hello "
但是,我只想删除parens中包含子字符串“John”的文本。那么比如说,

string = "Hello (Sally)"
(magic regex)
string = "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

这应该是你想要的要点:

>>> 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

这应该是你想要的要点:

>>> 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

这应该是你想要的要点:

>>> 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 "

如果要删除John的所有实例,可以执行以下操作:

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

如果要删除John的所有实例,可以执行以下操作:

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

如果要删除John的所有实例,可以执行以下操作:

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

您好(约翰·洛尔爵士)?你也试过什么吗?
你好(约翰·洛尔爵士)
?你也试过什么吗?
你好(约翰·洛尔爵士)
?你也试过什么吗?
你好(约翰·洛尔爵士)
?你也试过什么吗?你必须改变点来排除括号,因为它们太贪婪了
re.sub((?s)\(*John.*\),,,,“你好(John)我的John(Sally)”
结果是
Hello
。我明白了。修正。快速修正:DOTALL模式允许点匹配换行符;其余的空白字符总是匹配的。你必须改变点以排除括号,因为它们太贪婪了
re.sub((?s)\(*John.*\),,,,“你好(John)我的John(Sally)”
结果是
Hello
。我明白了。修正。快速修正:DOTALL模式允许点匹配换行符;其余的空白字符总是匹配的。你必须改变点以排除括号,因为它们太贪婪了
re.sub((?s)\(*John.*\),,,,“你好(John)我的John(Sally)”
结果是
Hello
。我明白了。修正。快速修正:DOTALL模式允许点匹配换行符;其余的空白字符总是匹配的。你必须改变点以排除括号,因为它们太贪婪了
re.sub((?s)\(*John.*\),,,,“你好(John)我的John(Sally)”
结果是
Hello
。我明白了。修正。快速修正:DOTALL模式允许点匹配换行符;其余的空白字符总是匹配的。