fork download
  1. import re
  2.  
  3. lst = ['AB8372943.txt', 'test.pdf', '123485940.docx', 'CW2839502.txt', 'AB1234567.txt', '283AB.txt']
  4. lst2 = [s for s in lst if re.match(r"AB\d{7}\.txt\Z", s)]
  5. print(lst2)
Success #stdin #stdout 0.02s 9528KB
stdin
Standard input is empty
stdout
['AB8372943.txt', 'AB1234567.txt']