A. python中如何用正則表達式匹配漢字
name = re.search(r'導演: (.*?) 主演:.*? '.encode('utf-8'),text,re.S).group(1)
B. 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"))
C. Python 正則表達式 支持批量語料過濾中文字元之間的空格
|^
#encoding:UTF-8
importre
importsys
reload(sys)
sys.setdefaultencoding('utf-8')
source="你好啊hellohi"
usample=unicode(source,'utf8')
xx=u"((?<=[u4e00-u9fa5])s+(?=[u4e00-u9fa5])|回^答s+|s+$)"
temp=re.sub(xx,'',usample);
printtemp;
D. Python怎麼通過 正則表達式提取漢字
python有很多網頁解析的包啊,BeautifulSoup,lxml之類的都很好用,犯不著正則
舉個栗子:
frombs4importBeautifulSoup
text='<h1class="title">.....</h1>'
soup=BeautifulSoup(text)
printsoup.text
E. python如何從文本中篩選出帶指定漢字的句子
#coding=gbk
#下面就是代碼,測試了一下沒有問題
#python 2.7.5
def srch(fileName):
f = open(fileName,'r').read()
s = f.split('\n')
a0 = s[0]
for i in range(0,len(s)):
if len(s) == 1: #這一行我不知道有沒有用,判斷文本是否只有一行
if a0[:1] != '#':
print '0' #return 0
break
a = s[i]
if a[:1] == '#':
print '-1' #return -1
else:
print '0' #return 0
print srch('abc.txt') #abc.txt is your file
F. python 判斷文本中是否含有特定的漢字
def is_contain:
special_chinese = "xxxxx"
literal_string = "xxxxxxxxxx"
return special_chinese in literal_string
G. python中正則表達式怎麼過濾中文日期類型
^
defdouble(matched):
value=int(matched.group('value'))
if(value<10):
return"0"+str(value);
else:
returnstr(value);
s='《2017年制7月3日》';
s=re.sub('(?P<value>d+)',double,s);
s=re.sub(r'D','',s);
prints;
s='《2017年6月5日與6月12日合集》';
s=re.sub('(?P<value>d+)',double,s);
s=re.sub('與','-',s)
s=re.sub(r'[^d-]','',s);
prints;
H. 用C程或python去除文件中的除",""."外的符號,只留下漢字
# -*- coding: cp936 -*-
with open("out.txt") as file:
import string
import re
s = re.sub("(,|\.)","",string.punctuation) + u"《》"
s = "[%s]" % s
out = re.sub(s,"",rece(str.__add__,file.readlines()).decode('GB2312'))
with open("res.txt","w") as file:
file.write(out.encode('GB2312'))
不能消除"\"字元以及"《》",需要的話修改就行
I. 請教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()
J. python,用正則表達式匹配特定漢字
在Python的string前面加上『r』, 是為了告訴編譯器這個string是個raw string,不要轉意backslash '\' 。 例如,\n 在raw string中,是兩個字元,\和n, 而不會轉意為換行符。由於正則表達式和 \ 會有沖突,因此,當一個字元串使用了正則表達式後,最好在前面加上'r'。
在[]中
-長用來指定一個字元集,在這個字元集中的一個可以拿來匹配:[abc] [a-z]
-元字元在在字元集中不起作用
-在[]內用^表示補集,用來匹配不在區間范圍內的字元
s=r'aba' 匹配abc
s=r't[io]p' 匹配tip或者top
s=r't[a-z0-9A-Z]'匹配t+0-9或者a-z或者A-Z
[abc]表示「a」或「b」或「c」
[0-9]表示0~9中任意一個數字,等價於[0123456789]
[\u4e00-\u9fa5]表示任意一個漢字
[^a1<]表示除「a」、「1」、「<」外的其它任意一個字元
[^a-z]表示除小寫字母外的任意一個字元