python - Using regex on Charfield in Django -


i have model with

class dbf_att(models.model):     name = models.charfield(max_length=50, null=true) 

and i'd check later object.name match regex:

    if re.compile('^\d+$').match(att.name):         ret = 'integer'     elif re.compile('^\d+\.\d+$').match(att.name):         ret = 'float'     else:         ret = 'string'   return ret 

this return 'string' when of att.name should match regex.

thanks!

regex great, more simpler , readable use other approaches. example, how using builtin types check type

try:     att_name = float(att.name)     ret = "integer" if att_name.is_integer() else "float" except valueerror:     ret = "string" 

fyi, regex code works fine. might want inspect data being checked.

demo:

>>> import re >>> = re.compile('^\d+$') >>> b = re.compile('^\d+\.\d+$') >>> a.match('10') <_sre.sre_match object @ 0x10fe7eb28> >>> a.match('10.94') >>> b.match('10') >>> b.match('10.94') <_sre.sre_match object @ 0x10fe7eb90> >>> a.match("string") >>> b.match("string") 

Comments

Popular posts from this blog

ruby - Trying to change last to "x"s to 23 -

jquery - Clone last and append item to closest class -

c - Unrecognised emulation mode: elf_i386 on MinGW32 -