Regex similar to "^(?u)\w$", but without digits?

A

Andreas

Hello,

I'd like to create a regex that captures any unicode character, but
not the underscore and the digits 0-9. "^(?u)\w$" captures them also.
Is there a possibility to restrict an expression like "\w" to "\w
without [0-9_]"?
I'm using python 2.5.4

Thanks in advance,
Andreas
 
J

John Machin

Hello,

I'd like to create a regex that captures any unicode character, but
not the underscore and the digits 0-9.

[requirement 1]
"^(?u)\w$" captures them also.
Is there a possibility to restrict an expression like "\w" to "\w
without [0-9_]"?

[requirement 2]

The two requirements are not the same.
R1: [^0-9_] matches any character except the underscore and the digits
0-9
R2: To match "like \w except for underscore and digits 0-9", find
"negative lookbehind assertion" in the re docs.

I've omitted the ^, $ and (?u) because the above advice is general.

HTH,
John
 
M

Mark Tolonen

Andreas said:
Hello,

I'd like to create a regex that captures any unicode character, but
not the underscore and the digits 0-9. "^(?u)\w$" captures them also.
Is there a possibility to restrict an expression like "\w" to "\w
without [0-9_]"?

'(?u)[^\W0-9_]' removes 0-9_ from \w.

-Mark
 
A

Andreas Pfrengle

I'd like to create a regex that captures any unicode character, but
not the underscore and the digits 0-9. "^(?u)\w$" captures them also.
Is there a possibility to restrict an expression like "\w" to "\w
without [0-9_]"?

'(?u)[^\W0-9_]' removes 0-9_ from \w.

-Mark

Hello Mark,

haven't tried it yet, but it looks good!
@John: Sorry for being imprecise, I meant *letters*, not *characters*,
so requirement 2 fits my needs.

Regards,
Andreas
 
A

Andreas Pfrengle

I'd like to create a regex that captures any unicode character, but
not the underscore and the digits 0-9. "^(?u)\w$" captures them also.
Is there a possibility to restrict an expression like "\w" to "\w
without [0-9_]"?

'(?u)[^\W0-9_]' removes 0-9_ from \w.

-Mark

Hello Mark,

haven't tried it yet, but it looks good!
@John: Sorry for being imprecise, I meant *letters*, not *characters*,
so requirement 2 fits my needs.

Regards,
Andreas
 
M

Mark Tolonen

Andreas Pfrengle said:
I'd like to create a regex that captures any unicode character, but
not the underscore and the digits 0-9. "^(?u)\w$" captures them also.
Is there a possibility to restrict an expression like "\w" to "\w
without [0-9_]"?

'(?u)[^\W0-9_]' removes 0-9_ from \w.

-Mark

Hello Mark,

haven't tried it yet, but it looks good!
@John: Sorry for being imprecise, I meant *letters*, not *characters*,
so requirement 2 fits my needs.

Note that \w matches alphanumeric Unicode characters. If you only want
letters, consider superscripts(¹²³), fractions (¼½¾), and other characters
are also numbers to Unicode. See the unicodedata.category function and
http://www.unicode.org/Public/UNIDATA/UCD.html#General_Category_Values.

If you only want letters as considered by the Unicode standard, something
this would give you only Unicode letters (it could be optimized to list
ranges of characters):

u'(?u)[' + u''.join(unichr(n) for n in xrange(65536) if
ud.category(unichr(n))[0]=='L') + u']'

Hmm, maybe Python 3.0 with its default Unicode strings needs a regex
extension to specify the Unicode category to match.

-Mark
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,151
Latest member
JaclynMarl
Top