[英]Regex: remove 's from a string?
字符串輸入: 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))
如何從python's
刪除's
?
您提取一個或多個字母數字字符的所有匹配項。
用
\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))
這里有兩個選項。 第一個使用正則表達式,第二個使用字符串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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.