python - Find strings from text same pattern as given words -
here words
'x201i' 'b-442n-5' '00.55005.001' ... i want write python script learn pattern given words. fixed length, same special character @ same position, same type(digit or letter) @ same position.
so can find out similar strings like
'b304d' => 'letter|number|number|number|letter' 'e-472n-5' =>'letter|-|number|number|number|letter|-|number' any suggestion or hints?
you need define 2 seperate patterns matching both type of formats.
r'^(?:[a-za-z]\d{3}[a-za-z]|[a-za-z]-\d{3}[a-za-z]-\d)$' example:
>>> import re >>> s = ['x201i', 'b-442n-5', '00.55005.001'] >>> [i in s if re.match(r'^(?:[a-za-z]\d{3}[a-za-z]|[a-za-z]-\d{3}[a-za-z]-\d)$', i)] ['x201i', 'b-442n-5'] [a-za-z] matches letter , \d matches digit.
Comments
Post a Comment