當前位置:首頁 » 編程語言 » 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")

熱點內容
sqlserver建立視圖 發布:2025-05-14 13:11:56 瀏覽:483
搭建httpsgit伺服器搭建 發布:2025-05-14 13:09:47 瀏覽:253
新電腦拿回來我該怎麼配置 發布:2025-05-14 13:09:45 瀏覽:238
視頻伺服器新建ftp用戶 發布:2025-05-14 13:03:09 瀏覽:225
php花生 發布:2025-05-14 12:54:30 瀏覽:550
java人才 發布:2025-05-14 12:29:10 瀏覽:649
如何打開軟密碼 發布:2025-05-14 12:28:55 瀏覽:427
七牛存儲待遇 發布:2025-05-14 12:27:20 瀏覽:422
C語言a35a4a5 發布:2025-05-14 11:53:48 瀏覽:814
android隱藏item 發布:2025-05-14 11:43:56 瀏覽:328