user1050619
user1050619

Reputation: 20886

Regular expression passes number in the first byte

I am trying to check to make sure a variable name starts only with alphabet and trying the below code to do it,

But for some reason it's failing

>>> a='1'
>>> if re.search(r"/^[a-zA-Z][a-zA-Z0-9_]*$/",a):
...     print 'pass'
... else:
...     print 'fail'
...
fail
>>>

I need to make sure the first alphabet is not numeric and following characters are only letters, a-z, 0-9 and underscore _

Upvotes: 0

Views: 248

Answers (1)

BrenBarn
BrenBarn

Reputation: 251428

Don't include those slashes in your regex. The string should contain just the actual regex you want to match. (In Perl the slashes are used to delimit the regex, but in Python the string quotes delimit it.) Your regex will never match because it tries to match beginning-of-line (^) right after a slash.

Upvotes: 3

Related Questions