[英]How do I remove a string that starts with '@' and ends with a blank character by using regular expressions in Python?
So I have this text:所以我有这样的文字:
"@Natalija What a wonderful day, isn't it @Kristina123?" “@Natalija 多么美好的一天,不是 @Kristina123 吗?”
I tried to remove these two substrings that start with the character '@' by using re.sub function but it didn't work.我尝试使用 re.sub function 删除以字符“@”开头的这两个子字符串,但它没有用。
How do I remove the susbstring that starts with this character?如何删除以该字符开头的子字符串?
Try this regex:试试这个正则表达式:
import re
text = "@Natalija What a wonderful day, isn't it @Kristina123 ?"
t = re.sub('@.*? ', '', text)
print(t)
OUTPUT: OUTPUT:
What a wonderful day, isn't it ?
This should work.这应该有效。
Code:代码:
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: output:
XXX What a wonderful day, isn't it XXX ?
It's possible to do it with re.sub()
, it would be something like this:可以使用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 ?
Alternatively, this can also be done without using the module re.或者,这也可以在不使用模块 re 的情况下完成。 You can first split the sentence into words.您可以先将句子拆分为单词。 Then remove the words containing the @ character and finally join the words into a new sentence.然后删除包含@字符的单词,最后将这些单词连接成一个新句子。
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)
I think this would be work for you.我认为这对你有用。 The pattern @\w+?\s
will determine expressions which start with @ continued by one or more alphanumeric characters then finish with an optional white space.模式@\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.