简体   繁体   中英

Regular expression for letters, dash, underscore, numbers, and space

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

  1. begins with only a letter or a numeric digit, and
  2. only letter, space, dash, underscore and numeric digit are allowed
  3. all ending spaces are stripped

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:

  1. | doesn't mean alternation in a character class, it means a pipe character literally.

  2. You used + for your following characters, meaning one or more, so a one-character string like '_' wouldn't match.

  3. 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM