在UCS-2构建中,python内部在
U0000ffff代码点上为每个unipre字符使用2个代码单元。正则表达式需要与之配合使用,因此您需要使用以下正则表达式来匹配它们:
highpoints = re.compile(u'[uD800-uDBFF][uDC00-uDFFF]')
此正则表达式匹配使用UTF-16代理对编码的任何代码点(请参见UTF-16代码点U + 10000至U +
10FFFF)。
为了使它在Python UCS-2和UCS-4版本之间兼容,可以使用
try:/
except来使用一个或另一个:
try: highpoints = re.compile(u'[U00010000-U0010ffff]')except re.error: # UCS-2 build highpoints = re.compile(u'[uD800-uDBFF][uDC00-uDFFF]')
在UCS-2 python构建上的演示:
>>> import re>>> highpoints = re.compile(u'[uD800-uDBFF][uDC00-uDFFF]')>>> example = u'Some example text with a sleepy face: U0001f62a'>>> highpoints.sub(u'', example)u'Some example text with a sleepy face: '
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)