regex - Python regular expression match with file extension -


i want use python regular expression utility find files has pattern:

000014_l_20111026t194932_1.txt 000014_l_20111026t194937_2.txt ... 000014_l_20111026t194928_12.txt 

so files want have underscore '_' followed number (1 or more digits) , followed '.txt' extension. used following regular expression didn't match above names:

match = re.match('_(\d+)\.txt$', file) 

what should correct regex match file names?

you need use .search() instead; .match() anchors start of string. pattern otherwise fine:

>>> re.search('_(\d+)\.txt$', '000014_l_20111026t194928_12.txt') <_sre.sre_match object @ 0x10e8b40a8> >>> re.search('_(\d+)\.txt$', '000014_l_20111026t194928_12.txt').group(1) '12' 

Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -