Hi,
I have Django model and in one of the fields I need to store a regex string that I can later use.
class Foo(models.Model):name = models.CharField(max_length=30, unique=True)regex_string = models.TextField()
So for example, the regex_string field might be set to:
r'\d{2}'
I then try to retrieve this later, compile it as a regex expression and use it - however, it doesn't seem to work as planned:
>>> pattern = re.compile(ham.regex_string)>>> print(pattern.match("22"))None
Obviously if I pass the raw string literal in directly, it works fine:
>>> pattern = re.compile(r'\d{2}')>>> pattern.match("22")<_sre.SRE_Match object at 0x1505100>
If I actually print ham.regex_string, it returns:
u"r'\\d{2}'"
So it's a unicode string, but for some reason the backslashes are doubled-up?
What I actually need is a way to store a regex raw string literal, so that I can retrieve it later and use it in a regex.
Is there a better way of doing this?
Cheers,
Victor
-- You received this message because you are subscribed to the Google Groups "Django users" group.
To view this discussion on the web visit https://groups.google.com/d/msg/django-users/-/k7i1EJD2fVwJ.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
No comments:
Post a Comment