字符串输入:Python的编程:非常容易学习
期望输出:Python编程:非常容易学习
这是目前我所做的,但并不起作用:
import re
mystr = "Python's Programming: is very easy to learn"
reg = r'\w+'
print(re.findall(reg, mystr))
我该如何从Python
中去除's
?
字符串输入:Python的编程:非常容易学习
期望输出:Python编程:非常容易学习
这是目前我所做的,但并不起作用:
import re
mystr = "Python's Programming: is very easy to learn"
reg = r'\w+'
print(re.findall(reg, mystr))
我该如何从Python
中去除's
?
\b's\b
请看proof。
说明:
--------------------------------------------------------------------------------
\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))
replace
方法。import re
mystr = "Python's Programming: is very easy to learn"
reg = r"'s"
print(re.sub(reg, '', mystr))
# prints: Python Programming: is very easy to learn
print(mystr.replace("'s",''))
# prints: Python Programming: is very easy to learn
's
可能会导致副作用。尝试使用 'such an awesome language'
字符串。 - Ryszard Czechmystr = "'such an awesome language'"
。但这似乎极不可能。 - noah