开发者

How to match alphabetical chars without numeric chars with Python regexp?

Using Python module re, how to get the equivalent of the "\w" (which matches alphanumeric chars) WITHOUT matching the numeric characters (those which can be matched by "[0-9]")?

Notice that the basic need is to match any character 开发者_如何学编程(including all unicode variation) without numerical chars (which are matched by "[0-9]").

As a final note, I really need a regexp as it is part of a greater regexp.

Underscores should not be matched.

EDIT:

  • I hadn't thought about underscores state, so thanks for warnings about this being matched by "\w" and for the elected solution that addresses this issue.


You want [^\W\d]: the group of characters that is not (either a digit or not an alphanumeric). Add an underscore in that negated set if you don't want them either.

A bit twisted, if you ask me, but it works. Should be faster than the lookahead alternative.


(?!\d)\w

A position that is not followed by a digit, and then \w. Effectively cancels out digits but allows the \w range by using a negative look-ahead.

The same could be expressed as a positive look-ahead and \D:

(?=\D)\w

To match multiple of these, enclose in parens:

(?:(?!\d)\w)+
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜