7

I have a regular expression that matches alphabets, numbers, _ and - (with a minimum and maximum length).

^[a-zA-Z0-9_-]{3,100}$

I want to include whitespace in that set of characters.

According to the Python documentation:

Character classes such as \w or \S are also accepted inside a set.

So I tried:

^[a-zA-Z0-9_-\s]{3,100}$

But it gives bad character range error. How can I include whitespace in the above set?

dda
  • 6,030
  • 2
  • 25
  • 34
Nitish Parkar
  • 2,838
  • 1
  • 19
  • 22

3 Answers3

25

The problem is not the \s but the - which indicates a character range, unless it is at the end or start of the class. Use this:

^[a-zA-Z0-9_\s-]{3,100}$
Martin Ender
  • 43,427
  • 11
  • 90
  • 130
3

^[-a-zA-Z0-9_\s]{3,100}

_-\s was interpreted as a range. A dash representing itself has to be the first or last character inside [...]

dda
  • 6,030
  • 2
  • 25
  • 34
-1

You're on the right track, Add a second backslash to escape the slash, because the backslash is an escape character.

^[a-zA-Z0-9_\\-\\s]{3,100}$
David Lai
  • 814
  • 7
  • 13
  • 1
    Sorry for the downvote, @david-lai. I think this answer is inaccurate in that it doesn't solve the problem; despite working around the reported error. It might be better to remove your answer, imo. – Eric McLachlan Jan 08 '20 at 09:15