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...
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.