正则表达式(二)

表示数量

匹配多个字符的相关格式

字符 功能
* 匹配前一个字符出现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:?
  • 需求:匹配出,0到99之间的数字
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}
  • 需求:匹配出,8到20位的密码,可以是大小写英文字母、数字、下划线
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:|
  • 需求:匹配出0-100之间的数字
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:( )
  • 需求:匹配出163、126、qq邮箱之间的数字
 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:\
  • 需求:匹配出<html>hh</html>
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'

结束语

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

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容