简体   繁体   English

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

[英]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.这应该有效。

  • @ matches the character @ @ 匹配字符 @
  • \w+ matches any word character as many times as possible, so it stops at blank character \w+ 尽可能多地匹配任何单词字符,因此它在空白字符处停止

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 ?
  • @ matches the @ character @ 匹配 @ 字符
  • [a-zA-Z0-9] matches alphanumerical (uppercase and lowercase) [a-zA-Z0-9] 匹配字母数字(大写和小写)
  • "+" means "one or more" (otherwise it would match only one of those characters) “+”表示“一个或多个”(否则它只会匹配其中一个字符)
  • \s matches whitespaces \s 匹配空格

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.

相关问题 如何使用正则表达式删除python字符串中的十六进制值? - How do I remove hex values in a python string with regular expressions? 如何使用正则表达式从python中删除字符串中的标签? (不是HTML) - How to remove tags from a string in python using regular expressions? (NOT in HTML) Python正则表达式如何删除以 - 开头并以逗号结尾的句子末尾的字符串? - Python regex how to remove string at the end of sentence that starts with - and ends with a comma? 如何使用正则表达式将字符串与python中的数字匹配? - How do I match a string up to a number in python using regular expressions? 如何使用python正则表达式将String数据附加到某些位置? - How do I append String data to certain positions using python regular expressions? 如何使用正则表达式检测字符串中的符号? - How do I detect symbols in a string using regular expressions? 如何使用正则表达式处理这样的字符串? - How do I process a string such as this using regular expressions? 如何在python中使用正则表达式形成单独的块? - How do I form separate blocks using regular expressions in python? 如何使用Python中的正则表达式在页面中搜索文本? - How do I search for text in a page using regular expressions in Python? Python-如何使用正则表达式拆分字符串? - Python - How to split a string using regular expressions?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM