user1012451
user1012451

Reputation: 3433

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

Upvotes: 3

Views: 18304

Answers (1)

Ned Batchelder
Ned Batchelder

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:

  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.

Upvotes: 12

Related Questions