正则表达式(二)

2018-05-08  本文已影响0人  Li77159

表示数量

匹配多个字符的相关格式

字符 功能
* 匹配前一个字符出现0次或者无限次,即可有可无
+ 匹配前一个字符出现1次或者无限次,即至少有1次
? 匹配前一个字符出现1次或者0次,即要么有1次,要么没有
{m} 匹配前一个字符出现m次
{m,} 匹配前一个字符至少出现m次
{m,n} 匹配前一个字符出现从m到n次
示例1:*
import re 

ret = re.match('[a-z][A-Z]', 'Mm')
print(ret.group())
print('-'*20)
ret = re.match('[a-z][A-Z]', 'Abcdefghijkl')
print(ret.group())

>>>
Mm
--------------------
Abcdefghijkl
示例2:?
import re

ret = re.match('[1-9]?[0-99]', '7')
print(ret.group())
ret = re.match('[1-9]?[0-9]', '33')
print(ret.group())
ret = re.match('[1-9]?[0-9]', '09')
print(ret.group())

>>>
7
33
9
示例3:{m}
import re

ret = re.match([a-zA-Z0-9_]{6}', '12a3g45678')
print(ret.group())
print('-'*20)
ret = re.match([a-zA-Z0-9_]{8,20}', 'dfdfdffgdyf6567765796qqkdlgnlk86785').group()
print(ret)

>>>
12a3g4
---------------
dfdfdffgdyf656776579

表示边界

字符 功能
^ 匹配字符串开头
$ 匹配字符串结尾
\b 匹配一个单词的边界
\B 匹配非单词边界

匹配分组

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

ret = re.match("[1-9]?\d","8")
ret.group()
>>>
8

ret = re.match("[1-9]?\d","78")
ret.group()
>>>
78

# 不正确的情况
ret = re.match("[1-9]?\d","08")
ret.group()
>>>
0
# 修正之后的
ret = re.match('[1-9]?\d$', '08')
ret.group()
>>>
Traceback (most recent call last):
    File "<stdin>", line 1, in <moudle>
AttributeError: 'NoneType' objects has no attribute 'group'


# 添加|
ret = re.match("[1-9]?\d$|100","8")
ret.group()
>>>
8

ret = re.match("[1-9]?\d$|100","78")
ret.group()
>>>
78

ret = re.match("[1-9]?\d$|100","08")
ret.group()
>>>
Traceback (most recent call last):
    File "<stdin>", line 1, in <moudle>
AttributeError: 'NoneType' objects has no attribute 'group'

ret = re.match("[1-9]?\d$|100","100")
ret.group()
>>>
100
示例2:( )
 import re

ret = re.match("\w{4,20}@163\.com", "test@163.com")
ret.group()
>>>
test@163.com

ret = re.match("\w{4,20}@(163|126|qq)\.com", "test@126.com")
ret.group()
>>>
test@126.com

ret = re.match("\w{4,20}@(163|126|qq)\.com", "test@qq.com")
ret.group()
>>>
test@qq.com

ret = re.match("\w{4,20}@(163|126|qq)\.com", "test@gmail.com")
ret.group()
>>>
Traceback (most recent call last):
    File "<stdin>", line 1, in <moudle>
AttributeError: 'NoneType' objects has no attribute 'group'
示例3:\
import re
# 能够完成对正确的字符串的匹配
ret = re.match("<[a-zA-Z]*>\w*</[a-zA-Z]*>", "<html>hh</html>")
ret.group()
>>>
<html>hh< /html>

# 如果遇到非正常的html格式字符串,匹配出错
ret = re.match("<[a-zA-Z]*>\w*</[a-zA-Z]*>", "<html>hh</htmlbalabala>")
ret.group()
>>>
<html>hh< /htmlbalabala>

# 正确的理解思路:如果在第一对<>中是什么,按理说在后面的那对<>中就应该是什么

# 通过引用分组中匹配到的数据即可,但是要注意是元字符串,即类似 r""这种格式
ret = re.match(r"<([a-zA-Z]*)>\w*</\1>", "<html>hh</html>")
ret.group()
>>>
<html>hh< /html>

# 因为2对<>中的数据不一致,所以没有匹配出来
ret = re.match(r"<([a-zA-Z]*)>\w*</\1>", "<html>hh</htmlbalabala>")
ret.group()
>>>
Traceback (most recent call last):
    File "<stdin>", line 1, in <moudle>
AttributeError: 'NoneType' objects has no attribute 'group'
示例4:\number
import re

ret = re.match(r"<(\w*)><(\w*)>.*</\2></\1>", "<html><h1>www.baidu.com</h1></html>")
ret.group()
>>>
<html><h1>www.baidu.com< /h1>< /html>

ret = re.match(r"<(\w*)><(\w*)>.*</\2></\1>", "<html><h1>www.baidu.com</h2></html>")
ret.group()
>>>
Traceback (most recent call last):
    File "<stdin>", line 1, in <moudle>
AttributeError: 'NoneType' objects has no attribute 'group'
示例5:(?P<name>) (?P=name)

import re

ret = re.match(r"<(?P<name1>\w*)><(?P<name2>\w*)>.*</(?P=name2)></(?P=name1)>", "<html><h1>www.baidu.com</h1></html>")
ret.group()
>>>
<html><h1>www.baidu.com< /h1>< /html>

ret = re.match(r"<(?P<name1>\w*)><(?P<name2>\w*)>.*</(?P=name2)></(?P=name1)>", "<html><h1>www.baidu.com</h2></html>")
ret.group()
>>>
Traceback (most recent call last):
    File "<stdin>", line 1, in <moudle>
AttributeError: 'NoneType' objects has no attribute 'group'

结束语

如果您对这篇文章有什么意见或者建议,请评论与我讨论.
如果您觉得还不错的话~可以点个喜欢鼓励我哦.
如果您想和我一起学习,请毫不吝啬的私信我吧~

上一篇 下一篇

猜你喜欢

热点阅读