i trying write function compare value regex see if matches. problem have quite many regex similar 1 difference range {} e.g. ^[a-z]{0,500}$ & ^[a-z]{0,200}$ similar regex diff of range/repetition. trying solve problem of how deal these regex 1 function. far have written function. think there must option better have done below. should able deal if no max or min specified well.
def check(value, min=none, max=none): regex = "^[a-z]"+"{"+min+","+max+"}$" r= re.compile(regex) if r.match(value): return true else: return false
use min="0" , max="" instead (that way, construct valid ranges if left unspecified).
also, don't if condition: return true etc. - return match object - evaluate true if there match (and can stuff later if want to).
further, no need compile regex if you're using once.
def check(value, min="0", max=""): regex = "[a-z]{" + min + "," + max + "}$" return re.match(regex, value) also, i've removed ^ because it's implicit in re.match().
Comments
Post a Comment