繁体   English   中英

python-re:我如何匹配alpha字符

[英]python-re: How do I match an alpha character

如何将alpha字符与正则表达式匹配。 我想要一个位于\\w但不在\\d的字符。 我希望它兼容unicode,这就是为什么我不能使用[a-zA-Z]

你的前两句话相互矛盾。 “in \\w但不在\\d ”包括下划线。 我从你的第三句话中假设你不想要下划线。

在信封背面使用维恩图有助于。 让我们来看看我们不想要的东西:

(1)与\\w不匹配的字符(即不要求任何不是字母,数字或下划线的字符)=> \\W
(2)digits => \\d
(3)下划线=> _

所以我们不想要的是字符类[\\W\\d_]中的任何内容,因此我们想要的是字符类中的任何内容[^\\W\\d_]

这是一个简单的例子(Python 2.6)。

>>> import re
>>> rx = re.compile("[^\W\d_]+", re.UNICODE)
>>> rx.findall(u"abc_def,k9")
[u'abc', u'def', u'k']

进一步的探索揭示了这种方法的一些怪癖:

>>> import unicodedata as ucd
>>> allsorts =u"\u0473\u0660\u06c9\u24e8\u4e0a\u3020\u3021"
>>> for x in allsorts:
...     print repr(x), ucd.category(x), ucd.name(x)
...
u'\u0473' Ll CYRILLIC SMALL LETTER FITA
u'\u0660' Nd ARABIC-INDIC DIGIT ZERO
u'\u06c9' Lo ARABIC LETTER KIRGHIZ YU
u'\u24e8' So CIRCLED LATIN SMALL LETTER Y
u'\u4e0a' Lo CJK UNIFIED IDEOGRAPH-4E0A
u'\u3020' So POSTAL MARK FACE
u'\u3021' Nl HANGZHOU NUMERAL ONE
>>> rx.findall(allsorts)
[u'\u0473', u'\u06c9', u'\u4e0a', u'\u3021']

U + 3021(杭州数字1)被视为数字(因此它匹配\\ w)但似乎Python将“数字”解释为“十进制数字”(类别Nd),因此它与\\ d不匹配

U + 2438(圆形拉丁文小写字母Y)与\\ w不匹配

所有CJK表意文字都被归类为“字母”,因此匹配\\ w

无论上述3点中的任何一点是否值得关注,这种方法都是目前发布的最佳模块。 将来会出现像\\ p {letter}这样的语法。

关于什么:

\p{L}

您可以将此文档用作参考: Unicode正则表达式

编辑:似乎Python不处理Unicode表达式 看看这个链接: 使用Python正则表达式处理重音字符 - [AZ]只是不够好 (不再有效,链接到互联网档案)

另一个参考:


对于后代,以下是博客上的示例:

import re
string = 'riché'
print string
riché

richre = re.compile('([A-z]+)')
match = richre.match(string)
print match.groups()
('rich',)

richre = re.compile('(\w+)',re.LOCALE)
match = richre.match(string)
print match.groups()
('rich',)

richre = re.compile('([é\w]+)')
match = richre.match(string)
print match.groups()
('rich\xe9',)

richre = re.compile('([\xe9\w]+)')
match = richre.match(string)
print match.groups()
('rich\xe9',)

richre = re.compile('([\xe9-\xf8\w]+)')
match = richre.match(string)
print match.groups()
('rich\xe9',)

string = 'richéñ'
match = richre.match(string)
print match.groups()
('rich\xe9\xf1',)

richre = re.compile('([\u00E9-\u00F8\w]+)')
print match.groups()
('rich\xe9\xf1',)

matched = match.group(1)
print matched
richéñ

您可以使用以下表达式之一来匹配单个字母:

(?![\d_])\w

要么

\w(?<![\d_])

在这里我匹配\\w ,但检查[\\d_]之前/之后是不匹配的。

来自文档:

(?!...)
Matches if ... doesn’t match next. This is a negative lookahead assertion. For example, Isaac (?!Asimov) will match 'Isaac ' only if it’s not followed by 'Asimov'.

(?<!...)
Matches if the current position in the string is not preceded by a match for .... This is called a negative lookbehind assertion. Similar to positive lookbehind assertions, the contained pattern must only match strings of some fixed length and shouldn’t contain group references. Patterns which start with negative lookbehind assertions may match at the beginning of the string being searched.

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM