python - 使用正则表达式 python 替换

标签 python regex pattern-matching

我有这样一句话

s = " zero/NN  divided/VBD  by/IN  anything/NN is zero/NN"

我需要将所有带有标签的单词替换为 tags 。输出应该是

s = "NN VBD IN NN is NN"

我试过像这样使用正则表达式替换

tup = re.sub( r"\s*/$" , "", s)

但这并没有给我正确的输出。请帮忙

最佳答案

这给出了你想要的输出:

tup = re.sub( r"\b\w+/" , "", s)

\b 匹配一个单词边界,后跟 \w+ 至少一个单词字符 (a-zA-Z0-9_)至少是斜线。

关于python - 使用正则表达式 python 替换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9124883/

相关文章:

python - Tensorflow:从输入到输出的梯度计算

python - numpy.dot -> MemoryError, my_dot -> 非常慢,但有效。为什么?

python - 为什么即使在关闭临时文件后我仍然可以写入和读取它?

regex - Perl Regex 试图读取我评论中的宏

javascript - 正则表达式 - 匹配字符串中的任何数字

file - OOP 中处理不同文件类型的设计模式

algorithm - 从字符串集合中推断模板

python - 降低Python中字典暴力破解的复杂度

Python 2.6+ str.format() 和正则表达式

scala - Play 2.0 模板 - Scala `match` 和 `val` 不在 View 模板中编译