就其本质而言,正则表达式(或 RE)是一种小型的、高度专业化的编程语言,(在Python中)它内嵌在Python中,并通过 re 模块实现。正则表达式模式被编译成一系列的字节码,然后由用 C 编写的匹配引擎执行。

字符匹配(普通字符,元字符):

1 普通字符:大多数字符和字母都会和自身匹配
              >>> re.findall('alvin','yuanaleSxalexwupeiqi')
                      ['alvin']

2 元字符:. ^ $ * + ? { } [ ] | ( ) \

元字符之. ^ $ * + ? { }

 import re

 ret=re.findall('a..in','helloalvin')
print(ret)#['alvin'] ret=re.findall('^a...n','alvinhelloawwwn')
print(ret)#['alvin'] ret=re.findall('a...n$','alvinhelloawwwn')
print(ret)#['awwwn'] ret=re.findall('a...n$','alvinhelloawwwn')
print(ret)#['awwwn'] ret=re.findall('abc*','abcccc')#贪婪匹配[0,+oo]
print(ret)#['abcccc'] ret=re.findall('abc+','abccc')#[1,+oo]
print(ret)#['abccc'] ret=re.findall('abc?','abccc')#[0,1]
print(ret)#['abc'] ret=re.findall('abc{1,4}','abccc')
print(ret)#['abccc'] 贪婪匹配

注意:前面的*,+,?等都是贪婪匹配,也就是尽可能匹配,后面加?号使其变成惰性匹配

 ret=re.findall('abc*?','abcccccc')
print(ret)#['ab']

元字符之字符集[]:

 #--------------------------------------------字符集[]
ret=re.findall('a[bc]d','acd')
print(ret)#['acd'] ret=re.findall('[a-z]','acd')
print(ret)#['a', 'c', 'd'] ret=re.findall('[.*+]','a.cd+')
print(ret)#['.', '+'] #在字符集里有功能的符号: - ^ \ ret=re.findall('[1-9]','45dha3')
print(ret)#['4', '5', '3'] ret=re.findall('[^ab]','45bdha3')
print(ret)#['4', '5', 'd', 'h', '3'] ret=re.findall('[\d]','45bdha3')
print(ret)#['4', '5', '3']

元字符之转义符\

反斜杠后边跟元字符去除特殊功能,比如\.
反斜杠后边跟普通字符实现特殊功能,比如\d

\d  匹配任何十进制数;它相当于类 [0-9]。
\D 匹配任何非数字字符;它相当于类 [^0-9]。
\s  匹配任何空白字符;它相当于类 [ \t\n\r\f\v]。
\S 匹配任何非空白字符;它相当于类 [^ \t\n\r\f\v]。
\w 匹配任何字母数字字符;它相当于类 [a-zA-Z0-9_]。
\W 匹配任何非字母数字字符;它相当于类 [^a-zA-Z0-9_]
\b  匹配一个特殊字符边界,比如空格 ,&,#等

 ret=re.findall('I\b','I am LIST')
print(ret)#[]
ret=re.findall(r'I\b','I am LIST')
print(ret)#['I']

现在我们聊一聊\,先看下面两个匹配:

 #-----------------------------eg1:
import re
ret=re.findall('c\l','abc\le')
print(ret)#[]
ret=re.findall('c\\l','abc\le')
print(ret)#[]
ret=re.findall('c\\\\l','abc\le')
print(ret)#['c\\l']
ret=re.findall(r'c\\l','abc\le')
print(ret)#['c\\l'] #-----------------------------eg2:
#之所以选择\b是因为\b在ASCII表中是有意义的
m = re.findall('\bblow', 'blow')
print(m)
m = re.findall(r'\bblow', 'blow')
print(m)

元字符之分组()

 m = re.findall(r'(ad)+', 'add')
print(m) ret=re.search('(?P<id>\d{2})/(?P<name>\w{3})','23/com')
print(ret.group())#23/com
print(ret.group('id'))#

元字符之|

 ret=re.search('(ab)|\d','rabhdg8sd')
print(ret.group())#ab

re模块下的常用方法

 import re
#
re.findall('a','alvin yuan') #返回所有满足匹配条件的结果,放在列表里
#
re.search('a','alvin yuan').group() #函数会在字符串内查找模式匹配,只到找到第一个匹配然后返回一个包含匹配信息的对象,该对象可以
# 通过调用group()方法得到匹配的字符串,如果字符串没有匹配,则返回None。 #
re.match('a','abc').group() #同search,不过尽在字符串开始处进行匹配 #
ret=re.split('[ab]','abcd') #先按'a'分割得到''和'bcd',在对''和'bcd'分别按'b'分割
print(ret)#['', '', 'cd'] #
ret=re.sub('\d','abc','alvin5yuan6',1)
print(ret)#alvinabcyuan6
ret=re.subn('\d','abc','alvin5yuan6')
print(ret)#('alvinabcyuanabc', 2) #
obj=re.compile('\d{3}')
ret=obj.search('abc123eeee')
print(ret.group())#
 import re
ret=re.finditer('\d','ds3sy4784a')
print(ret) #<callable_iterator object at 0x10195f940> print(next(ret).group())
print(next(ret).group())

注意:

 import re

 ret=re.findall('www.(baidu|oldboy).com','www.oldboy.com')
print(ret)#['oldboy'] 这是因为findall会优先把匹配结果组里内容返回,如果想要匹配结果,取消权限即可 ret=re.findall('www.(?:baidu|oldboy).com','www.oldboy.com')
print(ret)#['www.oldboy.com']

补充:

 import re

 print(re.findall("<(?P<tag_name>\w+)>\w+</(?P=tag_name)>","<h1>hello</h1>"))
print(re.search("<(?P<tag_name>\w+)>\w+</(?P=tag_name)>","<h1>hello</h1>"))
print(re.search(r"<(\w+)>\w+</\1>","<h1>hello</h1>")) #匹配出所有的整数
import re #ret=re.findall(r"\d+{0}]","1-2*(60+(-40.35/5)-(-4*3))")
ret=re.findall(r"-?\d+\.\d*|(-?\d+)","1-2*(60+(-40.35/5)-(-4*3))")
ret.remove("") print(ret)

最新文章

  1. ConcurrentAsyncQueue 2014-09-07
  2. web在线打印,打印阅览,打印维护,打印设计
  3. Node.js的UnitTest单元测试
  4. SqlIte数据库并发性
  5. oracle 删除用户及相关表数据,释放磁盘空间
  6. 通过git rebase修改commit message
  7. 推送(PUSH)
  8. ssh原理
  9. pjax 历史管理 jQuery.History.js
  10. Event Handling in Spring
  11. Android开发视频学习(2)
  12. swift小结02-基础篇
  13. Binary Tree Zigzag Level Order Traversal——LeetCode
  14. 关于sqfa
  15. 受众定向-Topic Model
  16. Gradle 1.12用户指南翻译——第三十七章. OSGi 插件
  17. 什么是HTML DOM对象
  18. day 16 包,random,shutil
  19. hdu 5144 NPY and shot 物理+三分
  20. [webpack]——loader配置

热门文章

  1. HDU 4342——History repeat itself——————【数学规律】
  2. mongodb 用户权限控制
  3. HttpClient4.x工具获取如何使用
  4. Javascript的map与forEach的区别
  5. Java—封装
  6. Python3爬虫04(其他例子,如处理获取网页的内容)
  7. 用户管理的设计--3.jquery的ajax实现二级联动
  8. jquery对radio的操作汇总
  9. 获取文件绝对路径:__FILE__与$_SERVER[SCRIPT_FILENAME&#39;&#39;]的区别
  10. React怎么创建.babelrc文件