1.正则表达式的简介
1.1 概念
正则表达式是对字符串操作的一种逻辑公式,就是用事先定义好的一些特定字符、及这些特定字符的组合,组成一个“规则字符串”,这个“规则字符串”用来表达对字符串的一种过滤逻辑
1.2 正则表达式的应用场景
• 表单验证(例如 : 手机号、邮箱、身份证… )就比如注册账号 输入账号密码是 不能包含什么特殊符号 最小长度 最大长度
• 爬虫
2. 正则表达式对Python的支持
python 支持正则 表达式 它有一个re模块 就是专门来做re模块的
2.1 普通字符
字母、数字、汉字、下划线、以及没有特殊定义的符号,都是"普通字符"。正则
表达式中的普通字符,在匹配的时候,只匹配与自身相同的一(几)个字符。有几个字符就匹配几个字符。
例如:表达式c,在匹配字符串abcde时,匹配结果是:成功;匹配到的内容
是c;匹配到的位置开始于2,结束于3。(注:下标从0开始还是从1开始,因
当前编程语言的不同而可能不同)
match( )函数
`• match(pattern, string, flags=0)
• 第一个参数是正则表达式,如果匹配成功,则返回一个match对象,否则返回一个None
• 第二个参数表示要匹配的字符串
• 第三个参数是标致位用于控制正则表达式的匹配方式 如: 是否区分大小写,多行匹配等等
None为False
数字类型(整形,浮点。。。),0为False,其他为True
列表类型(list,tuple,。。。),长度为0的为False,其他为True
Map类型(字典),条目个数为0的为False,其他为True
任何其他类型的对象均为True
import re
'''
match()函数
• match(pattern, string, flags=0)
• 第一个参数是正则表达式,如果匹配成功,则返回一个match对象,否则返回一个None
• 第二个参数表示要匹配的字符串
• 第三个参数是标致位用于控制正则表达式的匹配方式 如: 是否区分大小写,多行匹配等等
'''
#在众多数据找到python
pattern='python'
s='python and java'
result=re.match(pattern,s)
if result:
print(result)
else:
print('匹配到了')
<re.Match object; span=(0, 6), match='python'>
首先我们知道被尖括号< >包裹的就是对象
这就是返回了一个math对象 span这个东西类似于切片 (0,6)包含左 不包含右 刚好6个
import re
'''
match()函数
• match(pattern, string, flags=0)
• 第一个参数是正则表达式,如果匹配成功,则返回一个match对象,否则返回一个None
• 第二个参数表示要匹配的字符串
• 第三个参数是标致位用于控制正则表达式的匹配方式 如: 是否区分大小写,多行匹配等等
'''
#在众多数据找到python
pattern='pythonn'
s='python and java'
result=re.match(pattern,s)
if result:
print(result)
else:
print('没有匹配到')
没有匹配到
import re
'''
match()函数
• match(pattern, string, flags=0)
• 第一个参数是正则表达式,如果匹配成功,则返回一个match对象,否则返回一个None
• 第二个参数表示要匹配的字符串
• 第三个参数是标致位用于控制正则表达式的匹配方式 如: 是否区分大小写,多行匹配等等
'''
#在众多数据找到python
pattern='pythonn'
s='pythonn and java'
result=re.match(pattern,s)
if result:
print(result)
else:
print('没有匹配到')
<re.Match object; span=(0, 7), match='pythonn'>
观察这个span 的变化 math 也由'python'变成了'pythonn'
有start end 从哪里开始 span( )就是范围的意思
import re
'''
match()函数
• match(pattern, string, flags=0)
• 第一个参数是正则表达式,如果匹配成功,则返回一个match对象,否则返回一个None
• 第二个参数表示要匹配的字符串
• 第三个参数是标致位用于控制正则表达式的匹配方式 如: 是否区分大小写,多行匹配等等
'''
#在众多数据找到python
pattern='pythonn'
s='pythonn and java'
result=re.match(pattern,s)
if result:
print(result.group())
print(result.start())
print(result.end())
print(result.span())
else:
print('没有匹配到')
D:\python\python.exe D:/LongProject/爬虫/day005/math函数.py
pythonn
0
7
(0, 7)
Process finished with exit code 0
start( )方法和end( )方法分别来获取匹配值的开始 和结束
2.2 元字符
正则表达式中使用了很多元字符,用来表示某些特殊的含义或功能
转义元字符
一些无法书写或者具有特殊功能的字符,采用在前面加斜杠""进行转义的方法。
例如下表所示
尚未列出的还有问号?、星号*和括号等其他的符号。所有正则表达式中具有特殊含义的字符在匹配自身的时候,都要使用斜杠进行转义。这些转义字符的匹配用法与普通字符类似,也是匹配与之相同的一个字符
元字符的使用
小数点 .
. (这是一个点) 匹配除了换行符以外的任意一个字符
>>> import re
>>> re.match(r'a.c','abc').group()
'abc'
>>> re.match(r'a.c','abc')
<re.Match object; span=(0, 3), match='abc'>
>>>
>>> import re
>>> re.match(r'a.c','a5c').group()
'a5c'
>>> re.match(r'a.c','aKc').group()
'aKc'
>>> re.match(r'a.c','a\nc').group()
Traceback (most recent call last):
File "<pyshell#6>", line 1, in <module>
re.match(r'a.c','a\nc').group()
AttributeError: 'NoneType' object has no attribute 'group'
AttributeError:“NoneType”对象没有属性“group”
>>>
由上我们可以看出来group方法是将match对象的中的 match的值返回 而且这个点. 是能匹配除了换行符以外的任意一个字符的
>>> import re
>>> re.match(r'a.c','a你c').group()
'a你c'
>>> re.match(r'a.c','a你好呀c').group()
Traceback (most recent call last):
File "<pyshell#8>", line 1, in <module>
re.match(r'a.c','a你好呀c').group()
AttributeError: 'NoneType' object has no attribute 'group'
>>>
>>> re.match('a|c','ab').group()
'a'
>>> re.match(a|c,'c8').group()
Traceback (most recent call last):
File "<pyshell#10>", line 1, in <module>
re.match(a|c,'c8').group()
NameError: name 'a' is not defined
>>> re.match('a|c',c8).group()
Traceback (most recent call last):
File "<pyshell#13>", line 1, in <module>
re.match('a|c',c8).group()
NameError: name 'c8' is not defined
所以可以看出来 pattern 与string 都得是字符串格式’ ’
r’ ‘就是 原始字符串 规避 r’ ’ 中的\n换行问题
用“r”表示内部的字符串默认不转义(下面会说到. 与\ .)
| 逻辑或操作
>>> import re
>>> re.match(r'a|c','ab').group()
'a'
>>> re.match(r'a|c','c8').group()
'c'
>>> re.match(r'a|c','噶a').group()
Traceback (most recent call last):
File "<pyshell#3>", line 1, in <module>
re.match(r'a|c','噶a').group()
AttributeError: 'NoneType' object has no attribute 'group'
>>> re.match(r'a|c','a但').group()
'a'
>>> re.match(r'a|c','噶a').group()
Traceback (most recent call last):
File "<pyshell#5>", line 1, in <module>
re.match(r'a|c','噶a').group()
AttributeError: 'NoneType' object has no attribute 'group'
>>> re.match(r'a|c','a噶').group()
'a'
>>> re.match(r'a|c','但a').group()
Traceback (most recent call last):
File "<pyshell#7>", line 1, in <module>
re.match(r'a|c','但a').group()
AttributeError: 'NoneType' object has no attribute 'group'
>>>
这个很有意思 我们可以理解这个match函数是拿string第一个字符去和 pattern中的第一个字符(或者是元字符,预定字符集中)匹配 如果没有匹配到 是不会再从string的第二个字符再匹配的
因为match函数如果匹配成功,则返回一个match对象,否则返回一个None 而None.group() 也就是’NoneType’对象没有’group’这个属性
当然如果换成search函数的话就不同 上述是match函数的特性
>>> import re
>>> re.search(r'a|c','噶a').group()
'a'
>>>
[ ]匹配字符集中的一个字符
[ ]匹配字符集中的一个字符
>>> import re
>>> re.match(r'[abc]','a')
<re.Match object; span=(0, 1), match='a'>
>>> re.match(r'[abc]','a').group()
'a'
>>> re.match(r'[abc]2','a').group()
Traceback (most recent call last):
File "<pyshell#4>", line 1, in <module>
re.match(r'[abc]2','a').group()
AttributeError: 'NoneType' object has no attribute 'group'
>>>
>>> import re
>>> re.match(r'[abc]2','a2').group()
'a2'
>>> re.match(r'[abc]2','a552').group()
Traceback (most recent call last):
File "<pyshell#6>", line 1, in <module>
re.match(r'[abc]2','a552').group()
AttributeError: 'NoneType' object has no attribute 'group'
>>>
>>> re.match(r'[abc]52','c52').group()
'c52'
>>>
此处的’[abc]2’ 含义为 匹配一个以abc 任何一个字符开头 以2为结尾的且长度为两个字符的string(字符串)
>>> import re
>>> re.match(r'速度与激情[12345678]','速度与激情6').group()
'速度与激情6'
>>> re.match(r'速度与激情[12345678]','速度与激情12').group()
'速度与激情1'
>>>
上面还是体现了match函数的一种特性 用string的第一个字符来和pattern的第一个匹配 直到匹配到了 string中的1 与 pattern中的[12345678] 还是接着用1与1匹配 也就是 [ ]只匹配字符集中的一个字符
[ ^ ]对字符集的求反,也就是上面[ ]的反操作。尖号必须在方括号里的最前面.
[^ ]对字符集的求反,也就是上面[ ]的反操作。尖号必须在方括号里的最前面.
>>> re.match(r'速度与激情[^12345678]','速度与激情9').group()
'速度与激情9'
>>> re.match(r'速度与激情[^12345678]','速度与激情2').group()
Traceback (most recent call last):
File "<pyshell#12>", line 1, in <module>
re.match(r'速度与激情[^12345678]','速度与激情2').group()
AttributeError: 'NoneType' object has no attribute 'group'
>>> re.match(r'速度与激情[^12345678]','速度与激情7').group()
Traceback (most recent call last)