20170502 匹配单个字符串

为了方便爬虫,最近学习了正则表达式,将学习的过程记录如下:

一、正则表达式的概念:

1.使用单个字符串来描述匹配一系列符合某个句子语法规则的字符串
2.是对字符串操作的一种逻辑公式
3.应用来处理文本和数据
4.表达过程:依次拿出表达式和文本中的字符比较,如果每一个字符都能匹配,则匹配成功,否则匹配失败
 
二、匹配单个字符串
import re

ma = re.match(r'a','a')#匹配a字符
print(ma)
print(ma.group())

mb = re.match(r'a','b')#匹配a字符
print(mb)#匹配不到

m = re.match(r'.','c')#匹配任意字符
print(m)
print(m.group())

mz = re.match(r'[a-z]','F',re.I)#匹配a到z中的任一字符,且忽视大小写
#或者mz = re.match(r'[a-zA-Z0-9]','F',re.I) 可以匹配a-z,A-Z,0-9中的任一字符
print(mz)
print(mz.group())

mbracket = re.match(r'[\w]','[a]')#由于[...]已经被定义为匹配字符集了,所以用该方法匹配中括号里面的a不行
print(mbracket)#
mbracket1 = re.match(r'\[[\w]\]','[a]')#匹配中括号里面的a需要用该种方法
print(mbracket1)#
print(mbracket1.group())

二、匹配多个字符串

import re
ma = re.match(r'[A-Z][a-z]','Bc')
print(ma)
print(ma.group())
mb = re.match(r'[A-Z][a-z]','B')#匹配出来为None ,不能匹配单个字符了
print(mb)
mz = re.match(r'[A-Z][a-z]*','Bcdascxzdsa')# *号匹配前一字符0次或无限次
print(mz)
print(mz.group())

m99 = re.match(r'[1-9]?[0-9]','78')#匹配0-99的任一数字
m9 = re.match(r'[1-9]?[0-9]','7')#?表示出现0次或者1次
print(m9)
print(m99)
print(m99.group())

m6 = re.match(r'[0-9a-zA-Z]{6}','dsaf21')#匹配单词字符6次
m6 = re.match(r'[\w]{6}','dsaf21')#或者使用\w
print(m6)

mail = re.match(r'[\w]{6,10}@163.com','abc1234@163.com')#匹配含有6-10个字符的163邮箱
print(mail)
print(mail.group())

四、匹配字符串开头或者结尾
import re
mail = re.match(r'[\w]{6,10}@163.com','abc1234@163.com1234')
print(mail.string) #这样也是可以匹配到的

ma1 = re.match(r'^[\w]{4,10}@(163|126).com$','abc1@126.com123')#S表示必须以@163|126.com结尾
print(ma1) #这样就没法匹配到字符串了
ma2 = re.match(r'^[\w]{4,10}@(163|126).com$','abc1@122.com')
print(ma2)#122.com也不能匹配到

ma3 = re.match(r'\Aimooc[\w]*','imoocpython')# \A表示必须以 imooc 开头
print(ma3.string)
ma4 = re.match(r'\Aimooc[\w]*','iimoocpython')
print(ma4)# iimooc就没法匹配到

五、常用函数search、findall、sub、split
1.#search查找字符串中匹配到的第一次出现的位置
import re
str1 = 'imooc videonum = 1000'
print(str1.find('1000')) #find只能查找指定字符串,如果字符串改变就不能查找
info = re.search(r'\d+',str1)#\d表示查找数字
print(info.group())
2.#find 查找所有匹配,并把匹配的值返回到列表中
str2 = 'c++=100,java=90,python=80'
info_search = re.search(r'\d+',str2)
info_findall = re.findall(r'\d+',str2)
print(info_search.group(),'\n',info_findall)#search只能匹配到一个值,而findall返回一个包含所有值的列表
print(sum(int(x) for x in info_findall))#使用列表解析进行求和

3.#sub用于替换字符串 sub(pattern,repl,string,count=0,flags=0)
①#当repl为字符串时
str1 = 'imooc videonum = 1000'
str2 = 'c++=100,java=90,python=80'
info1 = re.sub(r'\d+','1001',str1)
info2 = re.sub(r'\d+','1001',str2)
print(info1,'\n',info2)

②#当repl为函数时
def add1(pipei):#定义函数
val = pipei.group()# pipei是pattern从string中匹配到的值,需要使用group来调用这个match
num = int(val)+1
return str(num)#需要返回一个str,int型不能传回字符串
info_add1 = re.sub(r'\d+',add1,str1)
print(info_add1)

info_add2 = re.sub(r'\d+',add1,str2)# sub实现的是findall的功能,能对所有匹配到的字符进行处理
print(info_add2)
4.split根据匹配到的字符串来分割字符,返回分割字符串组成的列表
split(pattern,string,maxsplit=0,flag=0)
import re
str4 = 'imooc:C C++ Java Python'
info_split1 = re.split(r':| ',str4)#将字符串str4 按:或者空格 进行分割
print(info_split1)

str5 = 'imooc:C C++ Java Python,C#'
info_split2 = re.split(r':| |,',str5)
print(info_split2)#按照冒号: 空格 或者逗号,进行分割成列表


 
posted @ 2017-05-02 11:26  云ime  阅读(307)  评论(0编辑  收藏  举报