python正则表达式手记


----------re模块进行正则的使用----------

#result=re.match(正则表达式,要匹配的字符串):使用正则对字符串进行过滤从前面开始匹配
#result.group():将获得到的数据取出
#result=re.search(正则表达式,要匹配的字符串):使用正则对字符串进行过滤从后面开始匹配
#result==None:判断正则表达式是否获取到内容,如果为True,则没有获取到内容
#re.search(r'd+','my hight 177 cm').group():使用正则读字符串进行过滤从找到符合要求的字符开始匹配。
#re.findall(r'd+','my hight 177 cm my weight 100 kg'):获取字符串中所有符合正则条件的数据信息,并保存到一个列表中
#re.sub(r'd+','100','my high 177 cm'):获取字符串中所有符合正则条件的数据信息,并使用第二个位置上的数据信息对其进行数据的替换操作
第二个位置可以配合函数进行处理,return返回值为str类型
#re.split(r':| ','address:beijing xxx@126.com'):根据正则表示式提供的规则对字符串进行有效的切割操作。并将结果存储到对应的列表中

----------正则表达式单字符匹配----------

.:匹配任意字符
[]:匹配[]中列举的字符
d:匹配任意一个数字
D:匹配非数字,即不是数字
s;匹配空吧即 空格、tab键
S:匹配非空白
w:匹配单词字符,字母、数字、下划线
W:匹配非单词字符,字母、数字、下划线


----------正则表达式多个字符匹配----------
*:匹配前一个字符出现0次或者无限次,即可有可无
+:匹配前一个字符出现1次或者无限次,即只有有1次
?:匹配前一个字符出现1次或者0次,即要么有1次,要么没有
{m}:匹配前一个字符出现m次
{m,n}:匹配前一个字符出现从m到n次

---------匹配开头结尾----------
^:匹配字符串开头
$:匹配字符串结尾
:转义

---------匹配分组----------
|:匹配左右任意一个表达式
():将括号中字符作为一个分组
<num>:引用分组num匹配到的字符串
(?P<name>):分组起别名
(?P=name):引用别名为name分组匹配到的字符串

---------附:正则表达式的练习题---------

1、匹配网址

有一批网址:

http://www.interoem.com/messageinfo.asp?id=35
http://3995503.com/class/class09/news_show.asp?id=14
http://lib.wzmc.edu.cn/news/onews.asp?id=769
http://www.zy-ls.com/alfx.asp?newsid=377&id=6
http://www.fincm.com/newslist.asp?id=415

需要 正则后为:

http://www.interoem.com/
http://3995503.com/
http://lib.wzmc.edu.cn/
http://www.zy-ls.com/
http://www.fincm.com/

 代码实现:

 1 def testFirst():
 2     #要进行处理的数据
 3     strHtml='http://www.interoem.com/messageinfo.asp?id=35 http://3995503.com/class/class09/news_show.asp?id=14 http://lib.wzmc.edu.cn/news/onews.asp?id=769 http://www.zy-ls.com/alfx.asp?newsid=377&id=6 http://www.fincm.com/newslist.asp?id=415'
 4     
 5     #strHtml='http://www.interoem.com/messageinfo.asp?id=35'
 6     
 7 
 8     print("转化前对应的数据:%s"%strHtml)
 9 
10     #进程正在表达式处理
11     result=re.findall("(http://.*?.(com|cn)/)",strHtml)
12 
13     #测试
14     #result = re.match("http://.*.(com|cn)/",strHtml).group()
15 
16     #创建一个变量,进行结果的存储
17     strResult=''
18 
19     #变量结果
20     for item in result:
21         strResult+=item[0]+" "
22 
23     #打印出结果
24     print ("转化后对应的数据:%s"%strResult)
testOne

2、 匹配所有合法的Python标识符

实现代码:

 1 #引用对应的包
 2 import re
 3 
 4 import keyword
 5 
 6 #2. 匹配所有合法的Python标识符
 7 def testFive():
 8     #获取到python中关键字的列表
 9     keyList=keyword.kwlist
10 
11     strKey="("+'|'.join(keyList)+")"
12 
13     #获取待处理的数据
14     strTitle="int main str wfwfwfwfdsfstr andand ifwhile"
15 
16     #打印待处理的数据
17     print("处理前的数据:%s"%strTitle)
18     
19     #进行正则的处理
20     result=re.findall(strKey,strTitle)
21     
22     #打印处理后的数据
23     print ("处理后的数据:%s"%str(result))
testTwo

3、匹配合法的ip地址

代码实现:

 1 引用包
 2 import re
 3 
 4 #3. 匹配合法的ip地址
 5 def testSex():
 6     #接受用户输入的ip地址
 7     strTitle=raw_input("请输入要进行判断的ip地址:")
 8 
 9     strRe=''
10     strRe+='([1-9]{1,2}|1[0-9]{2}|2[0-4][0-9]|25[0-5])'#第一位
11     strRe+='.'
12     strRe+='([1-9]?[0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])'#第二位
13     strRe+='.'
14     strRe+='([1-9]?[0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])'#第三位
15     strRe+='.'
16     strRe+='([1-9]?[0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$'#第四位
17     
18     #进行ip是否合法的判断
19     result=re.match(strRe,strTitle)
20 
21     if result==None:
22         print("匹配失败!")
23     else:
24         print("匹配成功!")
testThree
原文地址:https://www.cnblogs.com/qingtianyu2015/p/5854845.html