Improve company productivity with a Business Account.Sign Up

x
Solved

# Regular expression

Posted on 2006-11-22
Medium Priority
499 Views
Last Modified: 2012-06-27
Hi,
How do i find out if a string contains any non alpha numeric characters using regular expression, in python?

Thanks

0
Question by:jyotishb
2 Comments

LVL 14

Expert Comment

ID: 18000437
import re
def has_non_alpha_num(s):
return bool(re.search(r'[^a-zA-Z0-9]', s))

print has_non_alpha_num("123")  # Prints False
print has_non_alpha_num("abc")  # Prints False
print has_non_alpha_num("Hello!")  # Prints True
0

LVL 29

Accepted Solution

pepr earned 1500 total points
ID: 18001177
Also, you can use r'\W' special pattern sequence to match non-alphanum and not underscore characters (shortcut for [^a-zA-Z0-9_]). The alphanum plus underscore character can be expressed as r'\w' (shortcut for [a-zA-Z0-9_]). Depending on whether locale is set or not, the sequences consider ASCII letters or also 8-bit and unicode letters.

However, you can use also the method of the built-in string type which will probably be more efficient and takes into account also the 8-bit characters and Unicode strings.

print not s.isalnum()

Library reference -- 3.6.1 String Methods says:

isalnum( )
Return true if all characters in the string are alphanumeric and there is at least one character, false otherwise.
For 8-bit strings, this method is locale-dependent.

isalpha( )
Return true if all characters in the string are alphabetic and there is at least one character, false otherwise.
For 8-bit strings, this method is locale-dependent.

isdigit( )
Return true if all characters in the string are digits and there is at least one character, false otherwise.
For 8-bit strings, this method is locale-dependent.

0

## Featured Post

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

## Join & Write a Comment Already a member? Login.

"The time has come," the Walrus said, "To talk of many things: Of sets--and lists--and dictionaries-- Of variable kinks-- And why you see it changing not-- And why so strange are strings." This part describes how variables and references (see …
Introduction of Lists in Python: There are six built-in types of sequences. Lists and tuples are the most common one. In this article we will see how to use Lists in python and how we can utilize it while doing our own program. In general we can al…
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…
###### Suggested Courses
Course of the Month10 days, 8 hours left to enroll

#### 607 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.