這是我的代碼:
input_text_l = "hjahsdjhas pal sahashjas"
regex= re.compile(r"\s*\??(?:pal1|pal2|pal)\s*\??") #THIS IS THE REGEX THAT DOES NOT WORK CORRECTLY
if regex.search(input_text_l):
not_tag = " ".join(regex_tag.split(input_text_l))
#print(not_tag)
else:
pass
這是一個關于正則運算式應該如何作業的簡單圖表。

我希望你能幫我解決這個問題。
uj5u.com熱心網友回復:
不要做復雜的事情:\s …\s 就足夠了。 實際上正是您所寫的:“一個或多個,但不是沒有”
import re
re.search('\s pal\s ', input_text_l)
或幾種模式:
re.search('\s (?:pal1|pal2|pal)\s ', input_text_l)
例子:
>>> input_text_l = "hjahsdjhas pal sahashjas"
>>> re.search('\s (?:pal1|pal2|pal)\s ', input_text_l)
<re.Match object; span=(10, 15), match=' pal '>
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/352687.html
下一篇:嘗試對JSON資料使用gzip.compress時,為什么會出現“memoryview:abytes-likeobjectisrequired”?
