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?
\s | Matches whitespace characters, which include the \t , \n , \r , and space characters. \S | Matches non-whitespace characters.
\W means "non-word characters", the inverse of \w , so it will match spaces as well.
The metacharacter '\s' is used for matching whitespaces in python using regular expressions. The most common functions used in RegEx are findall(), search(), split(), and sub().
Decimal digit character: \d \d matches any decimal digit. It is equivalent to the \p{Nd} regular expression pattern, which includes the standard decimal digits 0-9 as well as the decimal digits of a number of other character sets.
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}$
^[-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 [...]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With