let's assume that I have string: abcNumber:"1234"ID:XXXYYY END
. How can I now check if this string contain phrase ID:...YYY
where ...
means that between ID
and YYY
can by anything (with 3 symbols)? I would like to get asnwer: True of False.
CodePudding user response:
You can use the re
module to do a regular expression search.
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!")
CodePudding user response:
Typical use case for regular expressions:
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