簡體   English   中英

正則表達式:從字符串中刪除 's?

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM