當前位置:首頁 » 編程語言 » python中文正則與

python中文正則與

發布時間: 2022-07-06 19:07:20

python 正則匹配中文問題

list列印是這樣的,這是python的系統行為,改變不了。
如果不想用循環,可以用 print(u"\n".join(items))

㈡ 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正則匹配漢字

#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正則表達式怎麼匹配中文

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

輸出
中文

前面加上

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

㈤ python 中文漢字正則替換問題

描述的不是特別清楚 。

re.sub("(?isu)省份_S+","省份",your_utf8_string)

上面應該可以的。 當然也可以變型,比如下面

re.sub(u"(?isu)省份_S{2}",u"省份",your_unicode_string)

㈥ python中文模式匹配,正則的問題

a='中國和美國政府就英國問題討論。'

import re

a1=re.sub('國','u3000國',a)

print(a1)

㈦ Python re函數 中文正則匹配

建議使用以下正則表達式:
(?<=>)[^a-zA-Z0-9_]+(?=<)

前面的 (?<=>) 和後面的 (?=<) ,使得匹配出現在> . . . < 之間;
[^a-zA-Z0-9_]+ 排除對英文數字下劃線的匹配,可根據具體情況作變動。

㈧ 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

importre


string=u"""<代碼>書名1【精裝版】<代碼>
<代碼>書名2【豪華版版】<代碼>
<代碼>書名3<代碼>"""

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


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

㈩ python 用正則匹配網頁中的中文字

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

熱點內容
滑板鞋腳本視頻 發布:2025-02-02 09:48:54 瀏覽:433
群暉怎麼玩安卓模擬器 發布:2025-02-02 09:45:23 瀏覽:557
三星安卓12彩蛋怎麼玩 發布:2025-02-02 09:44:39 瀏覽:743
電腦顯示連接伺服器錯誤 發布:2025-02-02 09:24:10 瀏覽:537
瑞芯微開發板編譯 發布:2025-02-02 09:22:54 瀏覽:147
linux虛擬機用gcc編譯時顯示錯誤 發布:2025-02-02 09:14:01 瀏覽:240
java駝峰 發布:2025-02-02 09:13:26 瀏覽:652
魔獸腳本怎麼用 發布:2025-02-02 09:10:28 瀏覽:538
linuxadobe 發布:2025-02-02 09:09:43 瀏覽:212
sql2000資料庫連接 發布:2025-02-02 09:09:43 瀏覽:726