讓我們假設我有 string: abcNumber:"1234"ID:XXXYYY END。我現在如何檢查此字串是否包含短語ID:...YYYwhere...意味著介于ID和YYY可以通過任何東西(帶有 3 個符號)?我想得到答案:真偽。
uj5u.com熱心網友回復:
您可以使用該re模塊進行正則運算式搜索。
import re
s = 'abcNumber:"1234"ID:XXXYYY END'
pat = re.compile(r'''
ID # a literal ID
.{3} # any three characters
YYY # a literal YYY''', re.X)
if re.search(pat, s):
print("Yep!")
uj5u.com熱心網友回復:
正則運算式的典型用例:
import re
s = 'abcNumber:"1234"ID:XXXYYY END'
match = re.search(r"ID:(.{3})YYY", s) # capture the group of 3 anthings between your markers
if match:
print(f"found: {match.group(1)}")
# found: XXX
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/356541.html
上一篇:通過php中的空格讀取字串
