unicode是python的内部编码。
字符串在Python内部的表示是unicode编码,因此,在做编码转换时,通常需要以unicode作为中间编码,即先将其他编码的字符串解码(decode)成unicode,再从unicode编码(encode)成另一种编码。
在编程中如果没有意识就可能会出bug。
像这样的unicode编码直接用中文进行正则表达式的匹配是没有结果的。因为使用的是utf8编码。
#!/usr/bin/env python
#coding=utf-8
import re
content = u'中文内容'
formula = '中(.*?)容'
pattern = re.compile(formula)
print(re.findall(pattern, content))
decode方法可以将某种编码转成unicode编码
encode方法可以将unicode编码转成另外的编码
加上一行content = content.encode('utf8')就可以了
#!/usr/bin/env python
#coding=utf-8
import re
content = u'中文内容'
content = content.encode('utf8')
formula = '中(.*?)容'
pattern = re.compile(formula)
print(re.findall(pattern, content))
另外编码入门可以看看这个