繁体   English   中英

如何使用 Python 中的正则表达式删除以“@”开头并以空白字符结尾的字符串?

[英]How do I remove a string that starts with '@' and ends with a blank character by using regular expressions in Python?

所以我有这样的文字:

“@Natalija 多么美好的一天,不是 @Kristina123 吗?”

我尝试使用 re.sub function 删除以字符“@”开头的这两个子字符串,但它没有用。

如何删除以该字符开头的子字符串?

试试这个正则表达式:

import re
text = "@Natalija What a wonderful day, isn't it @Kristina123 ?"
t = re.sub('@.*? ', '', text)
print(t)

OUTPUT:

What a wonderful day, isn't it ?

这应该有效。

  • @ 匹配字符 @
  • \w+ 尽可能多地匹配任何单词字符,因此它在空白字符处停止

代码:

import re

regex = r"@\w+"
subst = "XXX"

test_str = "@Natalija What a wonderful day, isn't it @Kristina123 ?"
result = re.sub(regex, subst, test_str, 0, re.MULTILINE)

print (result)

output:

XXX What a wonderful day, isn't it XXX ?

可以使用re.sub()来做到这一点,它会是这样的:

import re

text = "@Natalija What a wonderful day, isn't it @Kristina123 ?"

output = re.sub('@[a-zA-Z0-9]+\s','',text)

print(output) # Output: What a wonderful day, isn't it ?
  • @ 匹配 @ 字符
  • [a-zA-Z0-9] 匹配字母数字(大写和小写)
  • “+”表示“一个或多个”(否则它只会匹配其中一个字符)
  • \s 匹配空格

或者,这也可以在不使用模块 re 的情况下完成。 您可以先将句子拆分为单词。 然后删除包含@字符的单词,最后将这些单词连接成一个新句子。

if __name__ == '__main__':
    original_text = "@Natalija What a wonderful day, isn't it @Kristina123 ?"
    individual_words = original_text.split(' ')

    words_without_tags = [word for word in individual_words if '@' not in word]

    new_sentence = ' '.join(words_without_tags)
    print(new_sentence)


我认为这对你有用。 模式@\w+?\s将确定以@ 开头并接一个或多个字母数字字符然后以可选空格结尾的表达式。

import re

string = "@Natalija What a wonderful day, isn't it @Kristina123 ?"
pattern = '@\w+?\s'

replaced = re.sub(pattern, '', string)
print(replaced)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM