简体   繁体   English

如何删除以特定字符集开头的句子中的单词?

[英]How can I remove words in a sentence starting with a particular set of characters?

I want to use regular expression in Python to remove certain words in a sentence that start with a specific set of characters. 我想在Python中使用正则表达式来删除句子中以特定字符集开头的某些单词。

For example: If I have a string "searches for IPhone 5s search results" , I want to remove all the words that start with search. 例如:如果我有一个字符串"searches for IPhone 5s search results" ,我想删除所有以搜索开头的单词。 Thus, the result should be: "for IPhone 5s results" 因此,结果应该是: "for IPhone 5s results"

I used this: 我用过这个:

query=re.sub(r"/search\w+/", "", query) 

but this doesn't remove the es from searches 但是这并没有从搜索中删除es

query=re.sub(r"\bsearch\w+", "", query)

               ^^         ^^   

You dont need / here in python. 你不需要/这里在python中。

This would work for you 这对你有用

query=re.sub(r"\bsearch\w+", "", query)

\\b: sets up a boundary for the word search \\ b:为单词搜索设置边界

\\w+: captures the remaining words after search have been matched. \\ w +:搜索匹配后的剩余单词。

Hope this helps 希望这可以帮助

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

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