Solved

Regular expression

Posted on 2006-11-22
2
486 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
Comment
Question by:jyotishb
2 Comments
 
LVL 14

Expert Comment

by:RichieHindle
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

by:
pepr earned 500 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Python - How does this 2D array not work? 3 89
Need a python script 5 96
Difference of import usage in Python 1 76
Downgrading from Python 3.4.5 to 3.4.3 5 58
Installing Python 2.7.3 version on Windows operating system For installing Python first we need to download Python's latest version from URL" www.python.org " You can also get information on Python scripting language from the above mentioned we…
Sequence is something that used to store data in it in very simple words. Let us just create a list first. To create a list first of all we need to give a name to our list which I have taken as “COURSE” followed by equals sign and finally enclosed …
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…

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

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

Join & Ask a Question