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"))
Obviously if I pass the raw string literal in directly, it works fine:
>>> pattern = re.compile(r'\d{2}')
<_sre.SRE_Match object at 0x1505100>
If I actually print ham.regex_string, it returns:
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