如何匹配字母字符與正則表達式。我想要一個\w
但不在\d
中的字符。我想它與unicode兼容,這就是爲什麼我不能使用[a-zA-Z]
。python-re:如何匹配字母字符
回答
你的前兩句相互矛盾。 「在\w
但不在\d
」中包含下劃線。我從你的第三句話假設你不想強調。
在信封背面使用維恩圖有助於。讓我們來看看,我們不想要的東西:
(1)不受\w
匹配(即不希望任何非字母,數字和下劃線)字符=>\W
(2)位數= >\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(杭州數字一)被視爲數字(因此它匹配\ W),但它似乎Python的解釋「數字」表示‘十進制數字’(類別釹),所以它不匹配\ d
U + 2438(帶圈拉丁小寫字母Y)不匹配\ W
所有的中日韓表意文字被歸類作爲「字母」並因此匹配\ w
以上3點中的任何一點是否值得關注,該方法是最好的,您將從當前發佈的re模塊中獲得最佳效果。類似\ p {letter}的語法將來會出現。
謝謝! 儘管你提到的怪癖,我想我可以從這裏開始,看看我可以調整。 – basaundi 2010-01-11 08:18:50
什麼:
\p{L}
您可以使用此文件作爲參考:Unicode Regular Expressions
編輯:似乎Python doesn't handle Unicode expressions。看看這個鏈接:Handling Accented Characters with Python Regular Expressions -- [A-Z] just isn't good enough(不再有效時,鏈接到互聯網檔案館)
另一個引用:
- re.UNICODE
- python and regular expression with unicode
- Unicode Technical Standard #18: Unicode Regular Expressions
對於後代,這裏是博客上的例子:
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éñ
謝謝,但我無法知道一個字符是(CJK)標點符號還是0-9以外的數字符號,如果我執行一個範圍像\ u00E9- \ u00F8。 – basaundi 2010-01-10 23:55:42
你可以使用字母範圍,如果你引用一個像http://www.tamasoft.co.jp/en/general-info/unicode.html這樣的文檔,並選擇所有的字母間隔(這可能是無聊的... ... );這個鏈接也可以幫助你:http://kourge.net/projects/regexp-unicode-block – 2010-01-11 00:03:53
這個實例的例子在這裏會有所幫助。 – 2014-06-15 06:20:57
您可以使用下列表達式之一匹配的單個字母:
(?![\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.
- 1. 如何查找匹配字母(匹配字母后的mystring)
- 2. 如何調整Unicode字符大小以匹配字母字符
- 3. 匹配字母
- 4. 匹配任何unicode字母?
- 5. 如何使用PHP匹配字母數字和符號?
- 6. 如何使用正則表達式來匹配字母字符?
- 7. 正則表達式匹配字母數字字符和符號(&)
- 8. 如何匹配字符串?
- 9. 匹配數字和字母/數字
- 10. SQL(MySQL):匹配字符串中任何單詞的首字母?
- 11. Antlr4匹配西裏爾字母,拉丁字母,波蘭字母和希臘字母以及特殊字符?
- 12. 無法裏面QUERY_STRING通配符的字母數字字符串匹配ElasticSearch
- 13. 正則表達式的Java非字母數字字符匹配
- 14. 正則表達式匹配未知字母數字字符數
- 15. 匹配由空格分隔的字母數字字符
- 16. 匹配第一個字母數字字符跳過(The | An?)
- 17. 在nltk語法中匹配字母數字字符串
- 18. 如何編寫包含字母數字和特殊字符的輸入匹配
- 19. 匹配字符
- 20. 匹配字符
- 21. 匹配字符
- 22. PHP如何找到字符串後匹配/通配符/匹配
- 23. 匹配一個字母或數字
- 24. SQL字母數字模式匹配
- 25. 將幾個字母和字母與數字匹配
- 26. 任何語言的匹配字母
- 27. 匹配文字字符串'\ $'
- 28. 匹配連字符/破折號旁邊某些字母
- 29. 帶字母混淆矩陣的近似字符串匹配?
- 30. R:計算從字符串開始匹配多少字母
「兼容Unicode」 - 這是否意味着要E和匹配é,例如? – Seth 2010-01-10 23:46:09
在Python中,記住要指明一個unicode字符串,你必須使用這個:'u'Unicode string here'' - 假設你嘗試了str.find(),其中str是你的unicode字符串? – Alex 2010-01-11 00:11:24
我的意思是我想匹配一個,é,あ,日但不是1,。 (點),9,9等。例如, 。 – basaundi 2010-01-11 08:23:41