regex 如何使用正则表达式删除字符串及其对应的标点符号?

5rgfhyps  于 2023-05-08  发布在  其他
关注(0)|答案(1)|浏览(175)

我试图从字符串中删除单词"bro""girl",以及所有相应的标点符号和空格。例如,"What's up, bro? That's cool"变为"What's up? That's cool""What's up, that's cool"

pattern = r'\s*(bro|girl)[,.!?\s]*'
text = re.sub(pattern, ' ', text).strip()

我不是很精通正则表达式;这只对某些情况有效。它会错过字符串,如“你去女孩!去抓住他们”。

fcg9iug3

fcg9iug31#

您可以尝试以下模式:
pattern = r"\b(bro|girl)\b[\s\W]*"
这将选择所有匹配的单词“bro”和“girl”,这些单词由单词边界(使用\b命令)和任意数量的空格或非单词字符(使用[\s\W]* 命令)包围。检查以下案例的示例:

import re

text1 = "What's up, bro? That's cool"
text2 = "You go girl! Go get them"
pattern = r"\b(bro|girl)\b[\s\W]*"

result1 = re.sub(pattern, "", text1)
result2 = re.sub(pattern, "", text2)

print(result1) #What's up, That's cool
print(result2) #You go Go get them

相关问题