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]表示除小写字母外的任意一个字符