正则表达式快速入门(python示例)

正则表达式的作用我就不赘述了,首先讲解一下单个字符的匹配。

1、‘’.“(点):匹配除了(\n)之外的所有字符。

首先import re,以下的示例都默认已经导入了re包,不再另行说明。

[python]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. string1 = 'python'  
  2. string2 = '012345'  
  3. ma1 = re.match(r'.',string1)#r代表匹配元字符串,即正则表达式中的表达为最原始的含义,此时类似\n,不是换行,而是\和n两个字  
[python]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. ma2 = re.match(r'.',string2)  
  2. print ma1.group()#结果为p,只匹配第一个字符print ma2.group()#结果为0,只匹配第一个数字  
 

 
2、‘’[...]‘’(中括号):匹配中括号中的任意字符集。 
 

[python]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. ma = re.match(r'[p]',string1)  
  2. print ma.group()#匹配出p  
[python]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. ma = re.match(r'[a-z]',string1)#匹配a到z之间的字符  
  2. print ma.group()#p  
[python]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. string3 = 'PYTHON'  
  2. ma = re.match(r'[A-Z]',string3)#匹配A到Z之间的字符  
  3. print ma.group()#P  
[python]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. ma = re.match(r'[0-9]',string2)#匹配0到9之间的字符  
  2. print ma.group()#0  

 
[python]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. ma = re.match(r'[a-zA-Z0-9]',string1)#a-z、A-Z和0-9可组合使用  
  2. print ma.group()#p  

3、\d 和 \D :匹配数字/非数字

[python]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. string4 = '[];;:'  
  2. ma1 = re.match(r'\D',string4)#匹配非数字  
  3. ma2 = re.match(r'\d',string2)#匹配数字  
  4. print ma1.group()#[  
  5. print ma2.group()#0  

4、\s 和 \S 同上,分别匹配空白和非空白字符

5、\w 和 \W 同上,分别匹配单词字符[a-zA-Z0-9]和非单词字符

6、匹配带[ ]的字符集时,需要在正则表达式中加上转义符 \

7、* (星号):匹配0到无穷次

[python]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. ma = re.match(r'[a-z][a-z]*',string1)#小写字母开头,后接0个或者多个小写字母的字符串  
  2. print ma.group()#python  

8、+ (星号):匹配1到无穷次

[python]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. ma = re.match(r'[a-z][a-z]+',string1)#小写字母开头,后接1个或者多个小写字母的字符串  
  2. print ma.group()#python  

 
9、? (星号):匹配0到1次,即有或无 
 

[python]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. ma = re.match(r'[a-z][a-z]?',string1)#小写字母开头,后接0个或者1个小写字母的字符串  
  2. print ma.group()#py  

10、{m,n} :匹配出现m到n次的字符串

[python]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. ma = re.match(r'[\w]{1,4}',string1)任意字母和数字出现14次  
  2. print ma.group()#pyth  

11、*? 和 +? 和 ?? :非贪婪模式匹配,即最少匹配。仅举一例。

[python]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. ma = re.match(r'[\w][\w]+?',string1)#+最少匹配一次  
  2. print ma.group()#py  

12、^ 和 $ :分别匹配开始和结束。例如下例,可以成功匹配网易126的邮箱,但是如果string5='alibee@126.comabc'而

      没有$符号时,也能匹配成功,但此时的string5不是一个符合规则的网易邮箱账号。

[python]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. string5 = 'alibee@126.com'  
  2. ma = re.match(r'^[\w]{1,6}@126.com$',string5)  
  3. print ma.group()#alibee@126.com  

13、\A 和 \Z :分别匹配以指定的字符开始和结尾。下例表示以a开头的字符串,\Z的用法还没明白,有明白的朋友请解惑

[python]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. ma = re.match(r'\Aa[\w]+@126.com',string5)  
  2. print ma.group()  

14、| :表示或者,下例表示ab或者d都可以匹配

[python]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. ma = re.match(r'ab|d','d')  
  2. print ma.group()  

15、() :表示元组,下例表示即可匹配126邮箱,也可以匹配163邮箱

[python]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. string5 = 'alibee@126.com'  
  2. ma = re.match(r'alibee@(126|163).com',string5)  
  3. print ma.group()  

16、:匹配中文
[python]  view plain  copy
  1. ma = re.match(r'[\u4e00-\u9fa5] ','安定飞’)</span>  

以上基本包含了常用的正则表达式语法,下面简单介绍python中re包的几个方法。


1、search(pattern,string,flag):pattern中可以是正则表达式,flag 可以省略,search方法只能查找一个。

[python]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. ma = re.search(r'[\w]+',string5)  
  2. print ma.group()#alibee  

2、findall(pattern,string,flag) :找到所有符号pattern的字符串,flag可以省略。

[plain]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. string7 = '1,2,3,4'  
  2. ma = re.findall(r'\d+',string7)  
  3. print ma #['1', '2', '3', '4']  

3、sub(pattern,repl,string,count,flag):将repl替换成string,repl可以接字符串,也可以是函数返回值。

[python]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. string8 = '5'#接字符串  
  2. ma = re.sub(r'\d+','6',string8)  
  3. print ma #6  
[python]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. def addnumber(match) :  #接函数返回值  
  2.     num = match.group()  
  3.     num = int(num)+1  
  4.     return str(num)  
  5. ma = re.sub(r'\d+',addnumber,string8)  
  6. print ma #6  

4、split(pattern,string) :以pattern分割string,返回分割结果组成的列表。

[python]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. ma = re.split(r',',string7)  
  2. print ma #['1','2','3','4']  

以上的内容只是粗浅的学习正则表达式,更多用法请参考:https://msdn.microsoft.com/zh-cn/library/ae5bf541(VS.80).aspx


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值