import re patt=re.compile(r"[a-k][0369][a-zA-Z0-9#]*") m=patt.search("aaa9#") if(m!=None): print("Matched") else: print("Not Matched")