我想抓取噪声文本,其中有一个特定的模式:
text = "this is some text lskdfmd&@kjansdl and some more text sldkf&@lsakjd and some other stuff"
我想把这句话里的所有东西都去掉,在一个空格之后,在一个空格包含之前&;@。你知道吗
result = "this is some text and some more text and some other stuff"
一直在尝试:
re.compile(r'([\s]&@.*?([\s])).sub(" ", text)
不过,我好像没法理解第一部分。你知道吗
试试这个:
现在从所有单词的列表中删除
result
列表。你知道吗编辑1
编辑2建议@Pushpesh Kumar Rajwanshi
你可以用这个正则表达式来捕捉噪音串
用一个空格代替它。你知道吗
这里,
\s+
匹配任何空格,然后\S*
匹配零个或多个非空格字符,同时将&@
夹在其中,然后\S*
匹配零个或多个空格,最后紧跟着\s+
一个或多个空格,这些空格被空格删除,从而得到您想要的字符串。你知道吗另外,如果这个噪声字符串可以位于字符串的最开始或最末尾,可以随意将
\s+
更改为\s*
Regex Demo
Python代码
指纹
你可以用
见a demo on regex101.com。
在
Python
中:这就产生了
相关问题 更多 >
编程相关推荐