正则表达式

正则的组成:
原子,元字符,模式修正符 【其他语言中可能存在定界符】


#原子:
组成正则表达式的最小单位。一个正则表达式至少需要一个原子。
1.所有可见字符都是原子:a,b,c,d,A,B,C,D,你,我,他.+-*&^%##@
2.所有不可见字符也都是原子: \t \n \r ....
3.正则专用转义字符:

\d 表示0~9之间任意【一个】字符 -> [01234566789]自定义原子列表格式
\D 表示除了0~9之外的任意【一个】字符 -> [^0123456789] 自定义排除列表
\s 表示任意【一个】空白字符(不可见字符)->[ \n\r\t\v\f]自定义原子列表格式
\S 表示任意【一个】非空白字符(可见字符)->[^ \n\r\t\v\f]自定义排除列表
\w 表示0~9a~zA~Z和_中的任意【一个】字符 ->[a-zA-Z0-9_]自定义原子列表格式
\W 表示除了0~9a~zA~Z和_之外的任意【一个】字符 ->[^a-zA-Z0-9_]自定义排除列表
. 表示任意一个字符[除了\n]

#元字符:[原子修饰符]

[] 自定义原子列表
表示自定义原子列表中的任意一个字符,如果多个字符在ascii码中连续的,可以进行缩写,例如 [0123456789] -> [0-9]

[^] 自定义排除列表
表示原子列表之外的任意一个字符,,如果多个字符在ascii码中连续的,可以进行缩写

与数量相关的元字符
+ 表示1个以上的修饰的原子 {1,}
? 表示0个或者1个修饰的原子 {0,1}
* 表示人一个修饰的原子 {0,}
{m} 表示必须是m个修饰的原子
{m,n} 表示修饰的原子数量是m到n之间的数量都可以(包含m也包含n)
{m,} 表示修饰的原子数量是从m个到正无穷(包含m)
{,n} 表示0-n个修饰的原子(包含0也包含n)

\A 表示限定内容必须在整个字符串的开头部位
\Z 表示限定内容必须在整个字符串的结束部位
^ 表示限定内容必须在整个字符串的开头部位,支持多行匹配模式
$ 表示限定内容必须在整个字符串的结束部位,支持多行匹配模式

\b 表示能够当作英文单词分割的字符,(除了字母和数字 都是词边界,汉字作为字母处理)代表【一个】字符
\B 表示不能够当作英文单词分割的字符(非此边界,就是字母和数字)代表【一】个字符

| 表示选择关系,左右的内容二选一 非常重要

() 1.改变正则表达式中的优先级
2.将多个原子视为一个原子处理,方便使用元字符【可以叫做组或者模式单元】
3.将匹配的内容当作模式单元进行存储

 

额外补充:多行模式,如果字符串中包含\n字符,则可以在匹配的时候使用多行匹配模式。
多行匹配模式就是将每一行当作独立的字符串进行匹配。
注意:多行模式需要使用模式修正符


#模式修正符
设定匹配的一些额外的规则。
re.A 或者 re.ASCII 在ASCII偶是下进行正则匹配操作
re.U 或者 re.UNICODE 在UNICODE模式下进行正则匹配操作 默认情况下
re.S 使得原子.可以匹配任意字符包含\n
re.M 或者 re.MULTILINE 匹配操作在多行模式下进行 都是^和$相关
re.X 或者 re.VERBOSE 匹配的时候忽略正则表达式中的空格或者注释
re.I 或者 re.IGNORECASE 匹配过程中忽略大小写!

\转义字符的应用

字符串:\n \r \t \\ ...

因为在正则的元字符和原子当中,使用了部分的符号作为语法,为了匹配这些符号的单纯字符格式
在正则表达式用,添加\即可去掉意义,获取字符


扩展正则语法
1.(?Limsux) 多种模式修正符号的应用
2.(?:) 取消单元存储功能
3.(?P<name>) 自定义模式单元的名称
4.(?P=name) 获取自定义模式单元的内容
5.(?#) 正则注释内容,解析器不会处理
6.(?=) 正向先行断言 (零宽断言)
7.(?!) 负向先行断言 (零宽断言)
8.(?<=) 正向后行断言 (零宽断言)
9.(?<!) 负向后行断言 (零宽断言)
10.(?(id/name)Y|N) 根据模式单元是否存在决定使用Y的规则(模式单元存在)或者是N的规则(模式单元不存在)

 

#re模块:正则表达式专用模块,提供正则相关的操作方法

#re模块的方法
complie() 1.编译正则表达式获取正则表达式对象。2.可以循环利用,提高程序效率
格式:re.compile(正则字符串,模式修正符)
返回值:正则对象

escape() 对字符串进行转义处理(仅仅处理非数字和字母的字符串)
格式:re.escape(字符串)
结果:字符串

findall() 对字符串进行正则匹配 获取所有匹配的内容结果
格式:re.findall(正则表达式,匹配字符串,模式修正符)
结果:列表

finditer() 对字符串进行正则匹配 获取所有匹配的结果对象
格式:re.finditer(正则表达式,匹配字符串,模式修正符)
结果:包含所有匹配结果的迭代器

match()必须在字符串的开头,对字符串进行正则匹配,当匹配到第一个结果的时候就停止匹配,返回结果对象
格式:re.match(正则表达式,匹配字符串,模式修正符)
结果:匹配结果对象

search() 在任意位置,对字符串进行正则匹配,当匹配到第一个结果的时候就停止匹配,返回结果对象
格式:re.search(正则表达式,匹配字符串,模式修正符)
结果:匹配结果对象

split() 使用正则表达式切割字符串
格式:re.split(正则表达式,要切割的字符串,最大切割次数)
结果:切割之后的字符串组成的列表


sub() 正则替换
格式:re.sub(正则表达式,替换的字符串,匹配的字符串,替换次数)
结果:匹配字符串被替换之后字符串

subn()正则替换
格式:re.subn(正则表达式,替换的字符串,匹配的字符串,替换次数)
结果:由替换之后的字符串和替换次数组成的元组!


#正则对象的方法(借助compile方法获取) rx 代表正则对象

findall()使用正则表达式对象对指定的字符串进行匹配,获取所有匹配结果组成的列表
格式:rx.findall(要匹配的字符串)
结果:所有匹配结果组成的列表

finditer()使用正则表达式对象对指定的字符串进行匹配,获取所有匹配结果对象组成的迭代器
格式:rx.finditer(要匹配的字符串)
结果:迭代器

match()使用正则表达式对象对指定的字符串的开头进行匹配,返回第一次匹配到结果对象
格式:rx.match(要匹配的字符串)
结果:匹配结果对象

search()使用正则表达式对象对指定的字符串进行匹配,返回第一次匹配到结果对象
格式:rx.search(要匹配的字符串)
结果:匹配结果对象

split() 正则切割字符串
格式:rx.split(s)
结果:切割之后的字符组成的列表

sub() 正则替换
格式:rx.sub(要替换成的内容,被替换的字符串,替换次数)
结果:替换之后的字符串

subn()正则替换
格式:rx.sub(要替换成的内容,被替换的字符串,替换次数)
结果:替换之后的字符串和次数组成的元组

flags 获取当前正则对象的模式修正符
pattern 获取正则表达式的字符串格式
groups 模式单元的个数
groupindex

 

1.

 1 #正则表达式本质是字符串,单独使用没有意义。正则表达式需要配合正则相关的函数和方法
 2 
 3 #导入re模块
 4 import re
 5 
 6 #search方法:查找符合正则表达式的第一个内容
 7 #准备正表达式
 8 pattern = r'baidu'
 9 
10 #正则查找的字符串
11 url = 'http://www.baidu.com'
12 #匹配操作
13 result = re.search(pattern,url)
14 #获取对象中的span元组
15 var = tuple(result.span())
16 print(result)
17 #提取元组中的值
18 print('从第%s字符到第%s匹配'%var)
19 #获取对象中匹配成功的字符
20 print(result.group())
21 '''
22 输出结果:
23 <re.Match object; span=(11, 16), match='baidu'>
24 从第11字符到第16匹配
25 baidu
26 '''
27 
28 #原子测试
29 #准备正表达式
30 pattern = r'\d+'
31 
32 #正则查找的字符串
33 url = '23246468a'
34 
35 #匹配操作
36 result = re.search(pattern,url)
37 print(result)
38 #输出结果:<re.Match object; span=(0, 8), match='23246468'>

 

2.

 

 1 import re
 2 
 3 #准备正表达式
 4 #{,n} 表示0-n个修饰的原子(包含0也包含n)
 5 pattern = r'go{,9}gle'
 6 
 7 #正则查找的字符串
 8 '''
 9 google的网址:
10     www.google.com
11     www.gogle.com 
12     www.gooogle.com
13     www.goooogle.com
14     www.gooooogle.com
15 '''
16 
17 url = 'http://www.google.com'
18 
19 #匹配操作
20 result = re.search(pattern,url)
21 print(result)
22 #输出结果:<re.Match object; span=(11, 17), match='google'>

 

 

 3.

 1 import re
 2 #准备正表达式
 3 #():
 4 #   1.改变正则表达式中的优先级
 5 #   2.将多个原子视为一个原子处理,方便使用元字符【可以叫做组或者模式单元】
 6 #   3.将匹配的内容当作模式单元进行存储
 7 pattern = r'(go)+gle'
 8 
 9 #正则查找的字符串
10 #www.gogle.com   www.gogogle.com  www.gogogogle.com ...
11 url = 'http://www.gogogogogogleaaa.com'
12 
13 #匹配操作
14 result = re.search(pattern,url)
15 print(result)
16 #输出结果:<re.Match object; span=(11, 24), match='gogogogogogle'>

 

4.

 1 import re
 2 
 3 
 4 #re.A  和 re.U
 5 # 准 \w  表示0~9a~zA~Z和_中的任意【一个】字符 ->[a-zA-Z0-9_]自定义原子列表格式
 6 # +   表示1个以上的修饰的原子   {1,}
 7 pattern = r'\w+'
 8 
 9 #正则查找的字符串
10 url = '我love你他love我我也love大家'
11 
12 #匹配操作
13 #findall() :对字符串进行正则匹配
14 #           获取所有匹配的内容结果
15 #           格式:re.findall(正则表达式, 匹配字符串, 模式修正符)
16 #           结果:列表
17 #re.A 或者 re.ASCII    在ASCII偶是下进行正则匹配操作
18 result = re.findall(pattern,url,re.A)
19 print(result)
20 #输出结果:['love', 'love', 'love']
21 
22 #re.U 或者 re.UNICODE 在UNICODE模式下进行正则匹配操作 (默认,不加默认加re.U)
23 result = re.findall(pattern,url,re.U)
24 print(result)
25 #输出结果:['我love你他love我我也love大家']
26 
27 
28 #re.S  原子 . 使得原子.可以匹配任意字符包含\n
29 #  . 表示任意一个字符[除了\n]
30 pattern = r'e.b'
31 
32 #正则查找的字符串
33 url = 'google\nbaidu'
34 
35 #匹配操作
36 result = re.findall(pattern,url,re.S)
37 print(result)
38 #输出结果:['e\nb']
39 
40 
41 #re.M  或者 re.MULTILINE    匹配操作在多行模式下进行  都是^和$相关
42 
43 pattern = r'^www'
44 
45 #正则查找的字符串
46 url = 'www.baidu.com\nwww.google.com'
47 
48 #匹配操作
49 result = re.findall(pattern,url,re.M)
50 print(result)
51 #输出结果:['www', 'www']
52 
53 
54 #re.X 或者 re.VERBOSE   匹配的时候忽略正则表达式中的空格或者注释
55 
56 pattern = r'b   ai    du     '
57 
58 #正则查找的字符串
59 url = 'http://www.baidu.com'
60 
61 #匹配操作
62 result = re.findall(pattern,url,re.X)
63 print(result)
64 #输出结果:['baidu']
65 
66 
67 #re.I 或者 re.IGNORECASE 匹配过程中忽略大小写!
68 
69 pattern = r'baidu'
70 
71 #正则查找的字符串
72 url = 'http://www.BAIDU.com Baidu my baidu'
73 
74 #匹配操作
75 result = re.findall(pattern,url,re.I)
76 print(result)
77 #输出结果:['BAIDU', 'Baidu', 'baidu']

 

 5.

 1 #导入re模块
 2 import re
 3 
 4 #转义字符的应用
 5 #pattern = r'www\.baidu\.com'
 6 pattern = r'\[百度\]'
 7 #字符串
 8 #str = 'http://www.baidu.com'
 9 str = 'www.baidu.com[百度]'
10 
11 #进行匹配操作
12 result = re.search(pattern,str)
13 print(result)
14 #输出结果:<re.Match object; span=(13, 17), match='[百度]'>

 

6.

  1 import re
  2 '''
  3 #1.(?imx)模式修正符号的应用
  4 pattern = r'(?imx)bAi     dU'
  5 #字符串
  6 str = 'www.BaiDu.com'
  7 
  8 #进行匹配操作
  9 result = re.search(pattern,str)
 10 print(result)
 11 #输出结果:<re.Match object; span=(4, 9), match='BaiDu'>
 12 '''
 13 
 14 '''
 15 #2.(?:)      取消单元存储功能,节省资源
 16 pattern = r'bai(?:du)+'#将括号中的内容作为模式单元(自带)
 17 #字符串
 18 str = 'www.baidudududududu.com'
 19 
 20 #进行匹配操作
 21 result = re.search(pattern,str)
 22 print(result)#result匹配的结果对象
 23 #打印结果对象中的模式单元
 24 print(result.groups())
 25 '''
 26 '''
 27 输出结果:
 28 <re.Match object; span=(4, 9), match='baidu'>
 29 ()
 30 '''
 31 
 32 '''
 33 #3.(?P<name>) 自定义模式单元的名称
 34 #4.(?P=name)  获取自定义模式单元的内容
 35 pattern = r'<([a-z]+)>.*</\1>' #索引模式单元的应用
 36 #字符串
 37 str = '<title>百度</title><body>www.baidu.com</body>'
 38 #进行匹配操作
 39 result = re.search(pattern,str)
 40 print(result)#result匹配的结果对象
 41 #打印结果对象中的模式单元
 42 print(result.groups())
 43 '''
 44 '''
 45 输出结果:
 46 <re.Match object; span=(0, 17), match='<title>百度</title>'>
 47 ('title',)
 48 '''
 49 '''
 50 pattern = r'<(?P<biaoti>[a-z]+)>.*</(?P=biaoti)>'#字典模式单元的存储
 51 #字符串
 52 str = '<title>百度</title><body>www.baidu.com</body>'
 53 #进行匹配操作
 54 result = re.search(pattern,str)
 55 print(result)#result匹配的结果对象
 56 #打印结果对象中的模式单元
 57 print(result.groupdict())
 58 '''
 59 '''
 60 输出结果:
 61 <re.Match object; span=(0, 17), match='<title>百度</title>'>
 62 {'biaoti': 'title'}
 63 '''
 64 
 65 '''
 66 #5.(?#)      正则注释内容
 67 #正则表达式
 68 pattern = r'(?i)baidu(?#我就是个注释)'
 69 #字符串
 70 str = 'www.Baidu.com'
 71 #进行匹配操作
 72 result = re.search(pattern,str)
 73 print(result)
 74 #输出结果:<re.Match object; span=(4, 9), match='Baidu'>
 75 '''
 76 
 77 '''
 78 #6.(?=)      正向先行断言(零宽断言)=>指定匹配的内容【右侧】【有】什么标志内容
 79 #正向 ->有!   负向->没有
 80 #先行->向前/向右查找    后行->向后/向左查找
 81 pattern = r'itxdl(?=\.cn)' #itxdl后面就 .cn 的就匹配
 82 #字符串
 83 str = 'www.itxdl.com-www.itxdl.cn-www.itxdl.net-www.itxdl.org'
 84 #正则匹配操作
 85 result = re.search(pattern,str)
 86 print(result)
 87 #输出结果:<re.Match object; span=(18, 23), match='itxdl'>
 88 '''
 89 
 90 '''
 91 #7.(?!)      负向先行断言  (零宽断言)
 92 #正向 ->有!            负向->没有
 93 #先行->向前/向右查找    后行->向后/向左查找
 94 pattern = r'itxdl\d(?!\.cn)' # ?! 匹配项.cn的不给匹配
 95 #字符串
 96 str = 'www.itxdl1.com-www.itxdl2.cn-www.itxdl3.net-www.itxdl4.org'
 97 #正则匹配操作
 98 result = re.findall(pattern,str)
 99 print(result)
100 #输出结果:['itxdl1', 'itxdl3', 'itxdl4']
101 '''
102 
103 '''
104 #8.(?<=)     正向后行断言  (零宽断言)
105 #正向 ->有!            负向->没有
106 #先行->向前/向右查找    后行->向后/向左查找
107 pattern = r'(?<=mp4\.)itxdl' #?<=mp4 匹配项 itxdl 前面是mp4的就给予匹配
108 #匹配字符串
109 str = 'bbs.itxdl.cn-mp4.itxdl.cn-images.itxdl.cn-search.itxdl.cn'
110 #匹配操作
111 result = re.search(pattern,str)
112 print(result)
113 #输出结果:<re.Match object; span=(17, 22), match='itxdl'>
114 '''
115 
116 '''
117 #9.(?<!)     负向后行断言  (零宽断言)
118 #正向 ->有!            负向->没有
119 #先行->向前/向右查找    后行->向后/向左查找
120 pattern = r'(?<!mp4\.)itxdl\d' #?<!mp4 匹配项 itxdl 前面是mp4的就不给予匹配
121 #匹配字符串
122 str = 'bbs.itxdl1.cn-mp4.itxdl2.cn-images.itxdl3.cn-search.itxdl4.cn'
123 #匹配操作
124 result = re.findall(pattern,str)
125 print(result)
126 #输出结果:['itxdl1', 'itxdl3', 'itxdl4']
127 '''
128 
129 '''
130 #零宽断言注意事项
131 #1.同时存在前行与后行
132 pattern = r'(?<=www2\.)itxdl(?=\.cn)'
133 #字符串
134 str = 'www1.itxdl.com-www2.itxdl.cn-www3.itxdl.net-www4.itxdl.org'
135 #正则匹配操作
136 result = re.search(pattern,str)
137 print(result)
138 #输出结果:<re.Match object; span=(20, 25), match='itxdl'>
139 '''
140 
141 '''
142 #2.后行注意事项(字符宽度必须指定,不能是可变宽度)
143 
144 pattern = r'(?<=bbb\.)itxdl'
145 #字符串
146 str = 'aaa.itxdl.cn-bbb.itxdl.com-ccc.itxdl.org'
147 result = re.search(pattern,str)
148 print(result)
149 #输出结果:<re.Match object; span=(17, 22), match='itxdl'>
150 '''
151 
152 '''
153 #10.(?(id/name)Y|N)   根据模式单元是否存在决定使用Y的规则(模式单元存在)或者是N的规则(模式单元不存在)
154 #根据网址最开始是否存在www,如果存在,获取整个网址,不存在获取前半部分(第一个.前面的内容)
155 pattern = r'(www)?(?(1).*)'
156 #字符串
157 str = 'www.itxdl.cn.com/ll/222'
158 #str = 'bbs.itxdl.cn'
159 result = re.search(pattern,str)
160 print(result)
161 #输出结果:<re.Match object; span=(0, 23), match='www.itxdl.cn.com/ll/222'>
162 '''

 

7.

  1 #正则表达式的书写
  2 import re
  3 """
  4 #1.用户名(简单的正则)
  5     #a.设定规则(有字母,必须3个字符以上,可以使用数字,不能使用特殊符号,可以使用_)
  6     #b.书写一些符合规则的实例
  7 '''
  8         conghao
  9         conghao666
 10         cong_hao
 11         cong_666
 12 '''
 13     #c.书写正则
 14     #d.验证正则(正确的内容 和错误的内容)
 15 
 16 #正则表达式
 17 pattern = r'^\w{3,}$'
 18 #检测字符串
 19 str = '666' #3个以上给才予匹配
 20 
 21 #匹配操作
 22 result = re.search(pattern,str)
 23 print(result)
 24 #输出结果:<re.Match object; span=(0, 3), match='666'>
 25 """
 26 
 27 """
 28 #2.ip地址
 29     #a.设定规则(四个数字段;每个数字段使用.分隔;数字范围0~255)
 30     #b.书写一些符合规则的实例
 31 '''
 32 192.168.0.1
 33 127.0.0.1
 34 0.0.0.0
 35 255.255.255.255
 36 '''
 37     #c.书写正则
 38     #d.正则验证
 39 
 40 #正则表达式
 41 #pattern = r'\d(?#1位数)|[1-9]\d(?#2位数)| 1\d{2}(?#3位数100-199)  | 2[0-4]\d(?#3位数200-249) | 25[0-5]  \.            168.0.1'
 42 #pattern = r'^((?:\d)|(?:[1-9]\d)|(?:1\d{2})|(?:2[0-4]\d)|(?:25[0-5]))\.$'
 43 pattern = r'^(((?:([1-9]?|1\d|2[0-4])\d)|(?:25[0-5]))\.){3}((?:([1-9]?|1\d|2[0-4])\d)|(?:25[0-5]))$'
 44 #检测字符串
 45 str = '255.199.255.255'
 46 #匹配操作
 47 result = re.search(pattern,str)
 48 print(result)
 49 #输出结果:<re.Match object; span=(0, 15), match='255.199.255.255'>
 50 """
 51 
 52 """
 53 #3.电话号码
 54     #a.设定规则(纯数字;11位数字,第2.3个号码有限制)
 55     #b.书写一些符合规则的实例
 56 '''
 57     13012345678
 58     13112345678
 59     ...
 60     13912345678
 61     14712345678
 62     15112345678
 63     ...
 64     15912345678
 65     17012345678
 66     ...
 67     17912345678
 68     18012345678
 69     ...
 70     18912345678
 71 '''
 72     #c.书写正则
 73     #d.验证正则
 74 
 75 #正则表达式
 76 pattern = r'1(47|[3578]\d)\d{8}'
 77 #检测字符串
 78 str = '14712345678'
 79 #匹配操作
 80 result = re.search(pattern,str)
 81 print(result)
 82 #输出结果:<re.Match object; span=(0, 11), match='14712345678'>
 83 """
 84 """
 85 #4.身份证
 86     #a.设定规则(18位内容;数字和字母x组成;x只能最后1位;部分内容需要符合年月日的需求)
 87     #b.书写一些符合规则的实例
 88 '''
 89         110345  19991225  123    9
 90         地址    出生日期  顺序码 效验码
 91         120234  20000509  123    x
 92 '''
 93     #c.书写正则
 94     #d.验证正则
 95 
 96 pattern = r'\d{6}(18|19|[2-9]\d)\d{2}(0[1-9]|1[0-2])(0[1-9]|[12]\d|3[01])\d{3}(?:\d|x)'
 97 #检测字符串
 98 str = '11034599990131123x'
 99 
100 #匹配操作
101 result = re.search(pattern,str)
102 print(result)
103 print(result.groups())
104 print(result.group())
105 '''
106 输出结果:
107 <re.Match object; span=(0, 18), match='11034599990131123x'>
108 ('99', '01', '31')
109 11034599990131123x
110 '''
111 """

 

 8.

 1 #导入re模块
 2 import re
 3 '''
 4 #compile() 编译正则表达式获取正则表达式对象
 5 pattern = r'\d{13}'
 6 print(pattern,type(pattern))
 7 #输出结果:\d{13} <class 'str'>
 8 
 9 pattern_obj = re.compile(pattern,re.I)
10 print(pattern_obj,type(pattern_obj))
11 #输出结果:re.compile('\\d{13}', re.IGNORECASE) <class 're.Pattern'>
12 '''
13 
14 '''
15 #escape() 对字符串进行转义处理
16 s = 'how old are you ? i am 25'
17 result = re.escape(s)
18 print(result)
19 #输出结果:how\ old\ are\ you\ \?\ i\ am\ 25
20 '''
21 
22 '''
23 #findall() 对字符串进行正则匹配 获取所有匹配的内容结果
24 pattern = r'baidu'
25 s = 'www.baidu.com-www.BAIDU.com-www.BaiDu.com'
26 result = re.findall(pattern,s,re.I) # re.I 或者 re.IGNORECASE 匹配过程中忽略大小写!
27 print(result)
28 #输出结果:['baidu', 'BAIDU', 'BaiDu']
29 '''
30 
31 """
32 #finditer() 使用正则表达式对象对指定的字符串进行匹配,获取所有匹配结果组成的列表
33 pattern = r'baidu'
34 s = 'www.baidu.com-www.BAIDU.com-www.BaiDu.com'
35 result = re.finditer(pattern,s,re.I)
36 print(result,type(result))
37 #输出结果:<callable_iterator object at 0x0000025BCC738588> <class 'callable_iterator'>
38 for i in  result:
39     print(i)
40 '''
41 输出结果:
42 <re.Match object; span=(4, 9), match='baidu'>
43 <re.Match object; span=(18, 23), match='BAIDU'>
44 <re.Match object; span=(32, 37), match='BaiDu'>
45 '''
46 """
47 
48 '''
49 #search() 使用正则表达式对象对指定的字符串进行匹配,返回第一次匹配到结果对象
50 pattern= r'itxdl'
51 s = 'bbs.itxdl.cn;mp4.itxdl.cn;www.itxdl.cn'
52 result = re.search(pattern,s)
53 print(result,type(result))
54 #输出结果:<re.Match object; span=(4, 9), match='itxdl'> <class 're.Match'>
55 '''
56 
57 '''
58 #match() 必须在字符串的开头,对字符串进行正则匹配,当匹配到第一个结果的时候就停止匹配,返回结果对象
59 pattern= r'itxdl'
60 s = 'itxdl.cn;mp4.itxdl.cn;www.itxdl.cn'
61 
62 result = re.match(pattern,s)
63 print(result,type(result))
64 #输出结果:<re.Match object; span=(0, 5), match='itxdl'> <class 're.Match'>
65 '''
66 
67 '''
68 #split() 使用正则表达式切割字符串
69 s = '10世9贫,凑得8两77钱6分5毫4厘,况且3心2意,1等下流'
70 pattern  = r'\d+'
71 
72 result = re.split(pattern,s)
73 print(result)
74 #输出结果:['', '世', '贫,凑得', '两', '钱', '分', '毫', '厘,况且', '心', '意,', '等下流']
75 '''
76 
77 '''
78 #sub() (正则表达式,替换的字符串,匹配的字符串,替换次数)结果:匹配字符串被替换之后字符串
79 s = '蜡笔小新的爸爸是野原广志,蜡笔小新的妈妈是野原美伢,蜡笔小新的妹妹是野原葵'
80 pattern = r'蜡笔小新'
81 replace_s = '野原心之助'
82 result = re.sub(pattern,replace_s,s,2)
83 print(result)
84 #输出结果:野原心之助的爸爸是野原广志,野原心之助的妈妈是野原美伢,蜡笔小新的妹妹是野原葵
85 '''
86 
87 '''
88 #subn()(正则表达式,替换的字符串,匹配的字符串,替换次数)结果:由替换之后的字符串和替换次数组成的元组!
89 s = '蜡笔小新的爸爸是野原广志,蜡笔小新的妈妈是野原美伢,蜡笔小新的妹妹是野原葵'
90 pattern = r'蜡笔小新'
91 replace_s = '野原心之助'
92 result = re.subn(pattern,replace_s,s,2)
93 print(result)
94 #输出结果:('野原心之助的爸爸是野原广志,野原心之助的妈妈是野原美伢,蜡笔小新的妹妹是野原葵', 2)
95 '''

 

9.

  1 import  re
  2 
  3 '''
  4 #findall()对字符串进行正则匹配 获取所有匹配的内容结果,findall(s,开始位置,结束位置)
  5 rx = re.compile(r'baidu',re.I)
  6 print(rx,type(rx))
  7 s = 'www.baidu.com;mp4.BAIDU.com;image.BaiDu.com'
  8 result = rx.findall(s)
  9 #输出结果:re.compile('baidu', re.IGNORECASE) <class 're.Pattern'>
 10 print(result)
 11 #输出结果:['baidu', 'BAIDU', 'BaiDu']
 12 '''
 13 
 14 """
 15 #finditer()对字符串进行正则匹配 获取所有匹配的结果对象,finditer(s,开始位置,结束位置)
 16 rx = re.compile(r'baidu',re.I)
 17 s = 'www.baidu.com;mp4.BAIDU.com;image.BaiDu.com'
 18 result = rx.finditer(s)
 19 print(result)
 20 #输出结果:<callable_iterator object at 0x00000212D3F68588>
 21 #遍历匹配的结果
 22 for i in result:
 23     print(i)
 24 '''
 25 输出结果:
 26 <re.Match object; span=(4, 9), match='baidu'>
 27 <re.Match object; span=(18, 23), match='BAIDU'>
 28 <re.Match object; span=(34, 39), match='BaiDu'>
 29 '''
 30 """
 31 
 32 '''
 33 #match()必须在字符串的开头,对字符串进行正则匹配,当匹配到第一个结果的时候就停止匹配,返回结果对象,match(s,开始位置,结束位置)
 34 rx = re.compile(r'baidu',re.I)
 35 print(rx,type(rx))
 36 #输出结果:re.compile('baidu', re.IGNORECASE) <class 're.Pattern'>
 37 s = 'baidu.com;mp4.BAIDU.com;image.BaiDu.com'
 38 result = rx.match(s)
 39 print(result)
 40 #输出结果:<re.Match object; span=(0, 5), match='baidu'>
 41 '''
 42 
 43 '''
 44 #search()在任意位置,对字符串进行正则匹配,当匹配到第一个结果的时候就停止匹配,返回结果对象,search(s,开始位置,结束位置)
 45 rx = re.compile(r'baidu',re.I)
 46 print(rx,type(rx))
 47 #输出结果:re.compile('baidu', re.IGNORECASE) <class 're.Pattern'>
 48 s = 'tieba.baidu.com;mp4.BAIDU.com;image.BaiDu.com'
 49 result = rx.search(s)
 50 print(result)
 51 #输出结果:<re.Match object; span=(6, 11), match='baidu'>
 52 '''
 53 
 54 '''
 55 #split()使用正则表达式切割字符串
 56 rx = re.compile(r'\d+')
 57 print(rx,type(rx))
 58 s = '10世9贫,凑得8两7钱6分5毫4厘,况且3心2意,1等下流'
 59 result = rx.split(s)
 60 #输出结果:re.compile('\\d+') <class 're.Pattern'>
 61 '''
 62 
 63 '''
 64 #sub()(正则表达式,替换的字符串,匹配的字符串,替换次数)
 65 rx = re.compile(r'的')
 66 print(rx,type(rx))
 67 s = '蜡笔小新的爸爸是野原广志,蜡笔小新的妈妈是野原美伢,蜡笔小新的妹妹是野原葵'
 68 result = rx.sub('の',s,2)
 69 print(result)
 70 #输出结果:蜡笔小新の爸爸是野原广志,蜡笔小新の妈妈是野原美伢,蜡笔小新的妹妹是野原葵
 71 '''
 72 
 73 '''
 74 #subn()(正则表达式,替换的字符串,匹配的字符串,替换次数)
 75 rx = re.compile(r'的')
 76 print(rx,type(rx))
 77 #输出结果:re.compile('的') <class 're.Pattern'>
 78 s = '蜡笔小新的爸爸是野原广志,蜡笔小新的妈妈是野原美伢,蜡笔小新的妹妹是野原葵'
 79 result = rx.subn('の',s)
 80 print(result)
 81 #输出结果:('蜡笔小新の爸爸是野原广志,蜡笔小新の妈妈是野原美伢,蜡笔小新の妹妹是野原葵', 3)
 82 '''
 83 
 84 '''
 85 #flags 获取当前正则对象的模式修正符
 86 rx = re.compile(r'(?sx)(?P<a>\w+)(\d)(?P<b>\S)',)
 87 print(rx.flags)
 88 #输出结果:112
 89 
 90 #pattern 获取正则表达式的字符串格式
 91 print(rx.pattern)
 92 #输出结果:(?sx)(?P<a>\w+)(\d)(?P<b>\S)
 93 
 94 #groups 模式单元的个数
 95 print(rx.groups)
 96 #输出结果:3
 97 
 98 #groupindex
 99 print(rx.groupindex)
100 #输出结果:{'a': 1, 'b': 3}
101 '''
102 
103 '''
104 #正则对象的扩展用法(正则对象的匹配方法可以设置匹配的起始位置!)
105 s = 'http://www.itxdl.cn'
106 pattern = r'itxdl'#正则表达式
107 rx = re.compile(pattern)
108 result = rx.findall(s,10,18)
109 print(result)
110 #输出结果:['itxdl']
111 '''

 

实例

 1 # 导入re模块
 2 import re
 3 
 4 #接受匹配内容
 5 content = '0663-8888888'
 6 
 7 #匹配规则
 8 m=re.compile(r"^[1-9](\d{4,10})$")# QQ
 9 #m=re.compile(r"^1[34578](\d{9})$") #mobile
10 #m=re.compile(r"^0[1-9](\d{1,2})-[1-9](\d{6,7})$")#固定电话,"0571-1234431"
11 #m=re.compile(r"^[a-zA-Z][a-zA-Z0-9]{0,9}$") #user,"y"
12 #m=re.compile(r"^.{6,16}$" #password,"yinchengasdasdsadsadsadsad")
13 #m=re.compile(r"^((\d){1,3}.){3}((\d){1,3})$")  #简单ip,"125.0.0.3"
14 #m=re.compile(r"^(\d|[1-9]\d|1(\d){2}|2[0-4]\d|25[0-5])$","125")
15 #m=re.compile(r"^((\d|[1-9]\d|1(\d){2}|2[0-4]\d|25[0-5]).){3}(\d|[1-9]\d|1(\d){2}|2[0-4]\d|25[0-5])$")#复杂ip,"125.0.0.1"
16 #m=re.compile(r"^((18\d{2})|(19\d{2})|(20[0-1]\d))-(1[0-2]|0[1-9])-([0][1-9]|[1-2][0-9]|3[0-1])$")#出生日期,"1985-05-22"
17 #m=re.compile(r"^\w(.|_|\w)+@\w+(.\w+){1,3}$")#,"yincheng@163.com"
18 #m=re.compile(r"^\w(.|_|\w)+@\w+(.\w+){1,3}$")#,"yincheng.com_china@mails.tsinghua.edu.cn"
19 
20 res = m.search(content) #将匹配内容赋给变量
21 
22 if res is not None:
23     str1 = res.group()
24     print(str1)
25 else:
26     print(None)

 

posted @ 2019-05-22 20:11  双伟科技  阅读(454)  评论(0编辑  收藏  举报