我想用一個友好的unicode版本替換下面的正則表達式,該版本可以抓取像http://➡.ws和其他非ascii IRIs的東西。其目的是從用戶的文本中抓取這些文本,並將其編碼並將其html化爲真正的鏈接。蟒蛇unicode正則表達式
Python提供了一個re.UNICODE標誌,它改變了\ w的含義,但是在這種情況下(我可以看到)這不是超級有用,因爲它被定義爲「字母數字字符和下劃線」,而不是全部字符類包括下劃線。
domain_regex = re.compile(r"""
(
(https?://)
(
[0-9a-zA-Z]
[0-9a-zA-Z_-]*
\.
)+
[a-zA-Z]{2,4}
)
| # begins with an http scheme followed by a domain, or
(
(?<! # negative look-behind
[[email protected]]
)
(
[0-9a-zA-Z]
[0-9a-zA-Z_-]*
\.
)+
# top-level domain names
com|ca|net|org|edu|gov|biz|info|mobi|name|
us|uk|fr|au|be|ch|de|es|eu|it|tv|cn|jp
)
""", re.VERBOSE)
更多非ASCII域名:
- Bücher.ch - (。瑞士 - 德國 「書」,目前下)
-
- http://실례.테스트
這是http://stackoverflow.com/questions/1832893/python-regex-matching-unicode-properties的可能重複如果您需要更多幫助,請告知我們 – buckley 2012-03-22 22:08:50