Check python string format?

user1487000 picture user1487000 · Feb 19, 2013 · Viewed 47.1k times · Source

I have a bunch of strings but I only want to keep the ones with this format:

x/x/xxxx xx:xx

What is the easiest way to check if a string meets this format? (Assuming I want to check by if it has 2 /'s and a ':' )

Answer

kofemann picture kofemann · Feb 19, 2013

try with regular expresion:

import re
r = re.compile('.*/.*/.*:.*')
if r.match('x/x/xxxx xx:xx') is not None:
   print 'matches'

you can tweak the expression to match your needs