buk*_*zor 2 python regex unicode uri character-properties
我想用一个unicode友好的版本替换下面的正则表达式,它将捕获像http://➡.ws和其他非ascii IRI这样的东西.目的是从用户的文本中获取这些文本并对其进行编码并将其标记为真实链接.
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
[0-9a-zA-Z.@-]
)
(
[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)
Run Code Online (Sandbox Code Playgroud)
更多非ascii域名: