regex - Python re.search Patterns -
i have bunch of strings consist of q, d or t. below examples.
aa= "qddddqdtdqtd" bb = "qdt" cc = "tdqdqqdtqdq"
i new re.search, each string, possible patterns length start q, , ends d or q, , there no t in between first q , last d or q.
so aa, find "qddddqd"
for bb, find "qd"
for cc, find "qdqqd" , "qdq"
i understand basic forms of using re.search like:
re.search(pattern, my_string, flags=0)
just having trouble how set patterns mentioned above, how find patterns start q, or ends q, etc. appreciated!
use pattern follows describe: start q
, t [^t]*
, d or q [dq]
:
>>> import re >>> aa = "qddddqdtdqtd" >>> bb = "qdt" >>> cc = "tdqdqqdtqdq" >>> print(*(re.findall(r'q[^t]*[dq]', st) st in (aa,bb,cc)), sep='\n') ['qddddqd'] ['qd'] ['qdqqd', 'qdq']
Comments
Post a Comment