Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to match all alphanumeric except underscore on Python

Tags:

python

regex

I've been trying to match with regex all alphanumeric characters, except for the underscore. I'm currently using r"^[a-zA-Z0-9]*", but I wondered if it was possible to use \w and exclude _.

Thanks!

like image 610
nilsonneto Avatar asked May 22 '14 21:05

nilsonneto


People also ask

Is underscore alphanumeric in Python?

Python has a special sequence \w for matching alphanumeric and underscore. Please note that this regex will return true in case if string has alphanumeric and underscore.

What is the regex for underscore?

The _ (underscore) character in the regular expression means that the zone name must have an underscore immediately following the alphanumeric string matched by the preceding brackets. The . (period) matches any character (a wildcard).

How do you represent alphanumeric in regular expression?

For checking if a string consists only of alphanumerics using module regular expression or regex, we can call the re. match(regex, string) using the regex: "^[a-zA-Z0-9]+$".


1 Answers

Yes, like that: [^\W_]

Where \W is the opposite of \w

like image 114
Casimir et Hippolyte Avatar answered Oct 20 '22 01:10

Casimir et Hippolyte