首页 > 解决方案 > 正则表达式:从字符串中删除's?

问题描述

字符串输入:Python's Programming: is very easy to learn

预期输出:Python Programming: is very easy to learn

这是我到目前为止没有工作的内容:

import re
mystr = "Python's Programming: is very easy to learn"
reg = r'\w+'
print(re.findall(reg, mystr))

如何删除'sfrom python's

标签: pythonregex

解决方案


您提取一个或多个字母数字字符的所有匹配项。

利用

\b's\b

证明

说明

--------------------------------------------------------------------------------
  \b                       the boundary between a word char (\w) and
                           something that is not a word char
--------------------------------------------------------------------------------
  's                       '\'s'
--------------------------------------------------------------------------------
  \b                       the boundary between a word char (\w) and
                           something that is not a word char

蟒蛇代码

import re
mystr = "Python's Programming: is very easy to learn"
print(re.sub(r"\b's\b", '', mystr))

推荐阅读