Unicode正则表达式匹配汉字字符类 [英] Unicode regex to match a character class of Chinese characters
本文介绍了Unicode正则表达式匹配汉字字符类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
^ [一二三四五六七],
不匹配一,
但 ^一,
匹配一,
。
我的方式是指定汉字的字符类错误吗?
我从一个文件。
解决方案
适用于我,
code>>>> import re
>>> re.match(u'^ [一二三四五六七],',u'一,')
< _sre.SRE_Match对象; span =(0,2),match ='一',>
>>> (0)
'一,'
/ pre>
我想你没有将正则表达式定义为unicode字符串。
在python3中,
# - * - 编码:utf-8 - * -
import re
with open('file')as f:
reg = f.read()。strip()
print(re.match(reg,u'一,')。group 0))
^[一二三四五六七]、
doesn't match一、
But
^一、
matches一、
.Is my way of specifying a character class of Chinese characters wrong?
I read the regular expression from a file.
解决方案Works for me,
>>> import re >>> re.match(u'^[一二三四五六七]、', u'一、') <_sre.SRE_Match object; span=(0, 2), match='一、'> >>> re.match(u'^[一二三四五六七]、', u'一、').group(0) '一、'
I think you failed to define your regex as unicode string.
In python3, it would be
# -*- coding: utf-8 -*- import re with open('file') as f: reg = f.read().strip() print(re.match(reg, u'一、').group(0))
这篇关于Unicode正则表达式匹配汉字字符类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文