我正在尋找一個正則運算式來僅將特定單詞或正則運算式保留到我的字串中。這個正則運算式與我想保留的匹配
(DATE|ADDRESSE|phone|\d{1,}(\.\d{1,2}|\%))
例如,
輸入
s='this is an example from DATE and this is my phone and 5.50$'
期望的輸出
'DATE phone 5.50'
我試過了,'[^(DATE|ADDRESSE|phone|\d{1,}(\.\d{1,2}|\%))]'但它保留了字符而不是單詞。
你知道怎么做嗎?
uj5u.com熱心網友回復:
import re
s = "this is an example from DATE and this is my phone and 5.50$"
re_first = re.search(r"\bD\w ", s)
re_second = re.search(r"\bp\w ", s)
re_third = re.search(r"\d.*", s)
print(re_first.group(), re_second.group(), re_third.group())
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/488855.html
