howto combine regex special sequences?

Sean 'Shaleh' Perry shalehperry at home.com
Wed Oct 24 20:46:27 EDT 2001


On 25-Oct-2001 Graham Guttocks wrote:
> Greetings,
> 
> I'm trying to compile a regex so that I can test whether a string
> contains characters other than alphanumeric or whitespace.
> 
> I'm currently using this which seems to work:
> 
>   regex = re.compile(r'[^a-zA-Z0-9_ \t\n\r\f\v]')
> 
> I'd like to use the special sequences "\W" and "\S" for brevity
> however.  How can I rewrite the above regex using them instead?
> 
> I thought the following would work, but it doesn't:
> 
>   regex = re.compile(r'\W\S')
> 

>>> r = re.compile('[^\w\s]')
>>> r.match('7777')
>>> r.match('   ')
>>> r.match('dfgdfgdf')
>>> r.match('#%$#%#')
<re.MatchObject instance at 8083190>

> Another question: is there an easier/faster way to test whether a
> string contains characters other than alphanumeric or whitespace
> without using the re module, or am I on the right track?
> 

another possibility is:

import string
s = '1abD3'
for char in s:
    if char in string.digits:
        print char

Which will print all the digits in a string.  Of course this has the same
problem you have above.  A-Za-z works for English, but fails miserably for say
German.




More information about the Python-list mailing list