表示數(shù)量
匹配多個字符的相關(guān)格式
| 字符 |
功能 |
| * |
匹配前一個字符出現(xiàn)0次或者無限次,即可有可無 |
| + |
匹配前一個字符出現(xiàn)1次或者無限次,即至少有1次 |
| ? |
匹配前一個字符出現(xiàn)1次或者0次,即要么有1次,要么沒有 |
| {m} |
匹配前一個字符出現(xiàn)m次 |
| {m,} |
匹配前一個字符至少出現(xiàn)m次 |
| {m,n} |
匹配前一個字符出現(xià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}
- 需求:匹配出,8到20位的密碼,可以是大小寫英文字母、數(shù)字、下劃線
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
表示邊界
| 字符 |
功能 |
| ^ |
匹配字符串開頭 |
| $ |
匹配字符串結(jié)尾 |
| \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:( )
- 需求:匹配出163、126、qq郵箱之間的數(shù)字
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>
# 正確的理解思路:如果在第一對<>中是什么,按理說在后面的那對<>中就應(yīng)該是什么
# 通過引用分組中匹配到的數(shù)據(jù)即可,但是要注意是元字符串,即類似 r""這種格式
ret = re.match(r"<([a-zA-Z]*)>\w*</\1>", "<html>hh</html>")
ret.group()
>>>
<html>hh< /html>
# 因為2對<>中的數(shù)據(jù)不一致,所以沒有匹配出來
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'
結(jié)束語
如果您對這篇文章有什么意見或者建議,請評論與我討論.
如果您覺得還不錯的話~可以點個喜歡鼓勵我哦.
如果您想和我一起學(xué)習(xí),請毫不吝嗇的私信我吧~