python重新匹配unicode字符

时间:2011-03-30 21:55:50

标签: python regex unicode

在Unicode中将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

结果:

15°47'S 47°55'W
21º 18' N, 157º 51' W
32°46′58″N 96°48′14″W

似乎无法匹配unicode字符!

1 个答案:

答案 0 :(得分:5)

因为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",
]