Reputation: 3433
This is my attempt
def matcher(ex):
if re.match(r'^[\w|\d][A-Za-z0-9_-]+$', ex):
print 'yes'
My goal is to match only submission that satisfy all the followings
In my regex, matcher('__')
is considered valid. How can I modify to achieve what I want really want? I believe \w
also includes underscore. But matcher('_')
is not matched...
Upvotes: 3
Views: 18304
Reputation: 375494
def matcher(ex):
ex = ex.rstrip()
if re.match(r'^[a-zA-Z0-9][ A-Za-z0-9_-]*$', ex):
print 'yes'
Problems in your original regex:
|
doesn't mean alternation in a character class, it means a pipe character literally.
You used +
for your following characters, meaning one or more, so a one-character string like '_'
wouldn't match.
You used \w
in your first character, which accepted underscores.
Upvotes: 12