Reputation: 3793
I am doing some basic validation. The flow of the program goes like:
Now I want to make sure the following rules are fulfilled:
How can I do this using python/django regular expressions?
Please Help
Upvotes: 1
Views: 1407
Reputation: 63707
Try this
re.compile("^[A-Za-z]\w{2,}$")
>>> re.compile("^[A-Za-z]\w{2,}$")
<_sre.SRE_Pattern object at 0x0272C158>
>>> expr=re.compile("^[A-Za-z]\w{2,}$")
>>> expr.match("A12345")
<_sre.SRE_Match object at 0x02721288>
>>> expr.match("A1")
>>> expr.match("1AS")
>>> expr.match("AB1")
<_sre.SRE_Match object at 0x0272E138>
>>> expr.match("ab1")
<_sre.SRE_Match object at 0x02721288>
>>> expr.match("Abhijit$%^&#@")
>>>
Upvotes: 3
Reputation: 212835
You can do this in Python without regular expressions:
if a.isalnum() and a[0].isalpha() and len(filter(str.isdigit, a)) >= 3:
...
If according to @Toomai "3 digits" are "at least 3 characters long", then this is what you need:
if a.isalnum() and a[0].isalpha() and len(a) >= 3:
...
Upvotes: 6