howto combine regex special sequences?
lac at strakt.com
Thu Oct 25 15:19:22 CEST 2001
> 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')
> 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?
I've been up for 36 hours, but the expression you are looking for is
r = re.compile('[^\w\s]')
But you don't want to do that. Z is the last letter of the alphabet
in New Zealand, but Ö is the last letter here in Sweden. Use the
string methods instead.
More information about the Python-list