当前位置:首页 » 编程语言 » python匹配中文

python匹配中文

发布时间: 2022-05-04 08:02:22

python正则匹配汉字

#python2使用如下即可:

#encoding:UTF-8
importre
importsys
reload(sys)
sys.setdefaultencoding('utf-8')

defextract_number(input):
match=re.search(u"[u4e00-u9fa5]+",input)
returnmatch.group()

if__name__=="__main__":
printextract_number(unicode("dss2第三季度建安大sdssd43fds",'utf8'))#python3使用如下:


#encoding:UTF-8
importre

defextract_number(input):
match=re.search("[u4e00-u9fa5]+",input)
returnmatch.group()

if__name__=="__main__":
print(extract_number("dss2第三季度建安大sdssd43fds"))

② python正则表达式中要匹配汉字怎么弄

line = "000我是中国人111"
# matchMe = re.match("^[\u4e00-\u9fa5]+")
matchObj = re.match(r'.*([\u4e00-\u9fa5]+).*', line, re.L)
if matchObj:
print "matchObj.group() : ", matchObj.group()
print "matchObj.group(1) : ", matchObj.group(1)
print "matchObj.group(2) : ", matchObj.group(2)

else:
print "No match!!"

③ 如何用python匹配网页源码的中文字符

这跟系统学习无关,python的编码问题。

默认情况下,python会将未知的字符转换为unicode,再转换为相应平台的编码。


因为源网页的编码为GB2312,所以可以采用GB2312编码,而不幸的是u9fa5超出了范围,这个编码是中国自己弄出来的。用CP936编码可以解决这个问题。完整代码如下:

#-*-coding:utf-8-*-
importsys
importurllib2
importre
importurllib

keyword=u'来源:.+[u4e00-u9fa5]+'.encode('CP936')

html='http://finance.people.com.cn/money/n/2014/1009/c42877-25798373.html'
src=urllib2.urlopen(html).read()
math=re.compile(keyword)

list=re.findall(math,src)
forlineinlist:
printline

④ python 用正则匹配网页中的中文字

patt=re.compile(ur'<a.*href="(.*?)".*官方下载地址1.*</a>')

⑤ python 中正则表达式 怎么匹配中文

需要正则吗,if s1.find('中国') >= 0: 不就行了

⑥ python怎么用正则表达式提取中文

1、字符串line='ufeffD0002044x01大数据x01数据分析x01技术x01工具x01应用 '
想提取出其中的“大数据”,“数据分析”,“技术”,“工具”,“应用”这些中文,用了正则表达式:
>>>pat2='x01(.*?)'
>>>rs=re.compile(pat2).findall(line)
>>>print(rs)
['','','','','']
显示的结果是空,请问如何才能正确的提出中文部分。

2、原文: 法规名称:'《中华人民共和国合同法》',Items:[{法条名称:'第五十二条'
匹配成: 《中华人民共和国合同法》第五十二条
(?<=法规名称:').*?(',Items:[{法条名称:').*?(?=') 请问这样匹配哪里错了?Python报sre_constants.error: unterminated character set at position 22

3、Python re正则匹配中文,其实非常简单,把中文的unicode字符串转换成utf-8格式就可以了,然后可以在re中随意调用
unicode中中文的编码为/u4e00-/u9fa5,因此正则表达式u”[u4e00-u9fa5]+”可以表示一个或者多个中文字符
>>> import re
>>> s='中文:123456aa哈哈哈bbcc'.decode('utf8')
>>> s
u''
>>> print s
中文:123456aa哈哈哈bbcc 。

⑦ 请教python匹配中文字符的方法

#-*-coding:UTF-8-*-
__author__=u'丽江海月客栈'

s="""{"hearl":"","nickname":"","loginstatus":"","loginstate":"","tip":"未注册服务","idUser":"","sessionId":"","upgradeUrl":"","checkCodeKey":"false"}"""

ss=s.decode('utf-8')

importre


re_words=re.compile(u"[u4e00-u9fa5]+")
m=re_words.search(ss,0)
printm.group()

⑧ python正则表达式怎么匹配中文

#!/usr/bin/envpython
#_*_coding:utf-8_*_
a="中文"
printa

输出
中文

前面加上

#_*_ coding: utf-8_*_

⑨ python怎么用正则表达式提取中文

Python re正则匹配中文,其实非常简单,把中文的unicode字符串转换成utf-8格式就可以了,然后可以在re中随意调用
unicode中中文的编码为/u4e00-/u9fa5,因此正则表达式u”[\u4e00-\u9fa5]+”可以表示一个或者多个中文字符
>>> import re

>>> s='中文:123456aa哈哈哈bbcc'.decode('utf8')
>>> s
u'\u4e2d\u6587\uff1a123456aa\u54c8\u54c8\u54c8bbcc'
>>> print s
中文:123456aa哈哈哈bbcc

>>> re.match(u"[\u4e00-\u9fa5]+",s)
<_sre.SRE_Match object at 0xb77742c0>

>>> pat='中文'.decode("utf8")
>>> re.search(pat,s)
<_sre.SRE_Match object at 0x16a16df0>

>>> newpat='这里是中文内容'.decode("utf8")

>>> news=re.sub(pat,newpat,s)
>>> print news
这里是中文内容:123456aa哈哈哈bbcc

from:http://blog.aizhet.com/web/12078.html

⑩ Python 求正则表达式匹配中文

#coding=utf-8

importre


string=u"""<代码>书名1【精装版】<代码>
<代码>书名2【豪华版版】<代码>
<代码>书名3<代码>"""

fresult=re.findall(u">(.*?)[【|<]",string)


foriteminfresult:
printitem.encode("utf-8")

热点内容
方舟怎么用自己的存档进入别人的服务器 发布:2025-05-14 16:46:25 浏览:876
微博视频高清上传设置 发布:2025-05-14 16:38:41 浏览:548
数据库图书管理设计 发布:2025-05-14 16:33:52 浏览:378
php开发的网页 发布:2025-05-14 16:22:03 浏览:477
服务器内存跑满了怎么回事 发布:2025-05-14 16:21:16 浏览:224
微信qq音乐缓存 发布:2025-05-14 16:16:16 浏览:469
c语言回收内存 发布:2025-05-14 16:16:08 浏览:144
2021国产安卓顶级旗舰买哪个 发布:2025-05-14 16:15:36 浏览:300
linux自学视频 发布:2025-05-14 16:14:49 浏览:256
我的世界服务器崩了重启 发布:2025-05-14 16:09:37 浏览:45