ʞɔı*_*ɔıu 2 python regex unicode
无法在Python中将unicode字符与正则表达式匹配
# -*- coding: utf8 -*-
import re
locations = [
"15°47'S 47°55'W",
"21º 18' N, 157º 51' W",
"32°46?58?N 96°48?14?W",
]
rx = re.compile(ur"""
^\d+[°º]
|
^\d+[\xb0\xba]
""", re.X)
for loc in locations:
if not rx.match(loc):
print loc
Run Code Online (Sandbox Code Playgroud)
结果:
15°47'S 47°55'W
21º 18' N, 157º 51' W
32°46?58?N 96°48?14?W
Run Code Online (Sandbox Code Playgroud)
似乎无法匹配unicode chars!
因为locations不是unicode字符串.
locations = [
u"15°47'S 47°55'W",
u"21º 18' N, 157º 51' W",
u"32°46?58?N 96°48?14?W",
]
Run Code Online (Sandbox Code Playgroud)