Day18
一、正則表達式的模塊
什么是非貪婪:在正則中控制匹配不確定次數的符號后邊可以加?,來表示盡可能少的匹配
在貪婪的時候,盡可能多的匹配
注意:盡可能少是在能夠匹配的前提下盡可能少
1、 compile(正則表達式) --->
將正則表達式轉換成正則對象
編譯后可以直接通過對象調用相的對象方法
re_object = re.compile(r'\d{3}')
print(re_object)
2、fullmatch(正則表達式,字符串) -->
讓字符串和正則表達式完全匹配,匹配成功返回匹配對象,匹配失敗返回None
# 應用:檢測字符串內容是否符合要求,例如:檢測賬號密碼,判斷手機號,身份證號等是否合法
result = re.fullmatch(r'(\d{3})=([a-z]+)', '342=asda')
print(result)
3、 匹配對象
a.span - 匹配到的字符串在原字符串中的下標范圍(結果是元素)
# 獲取正則表達式中整個內容的范圍
print(result.span())
start, end = result.span()
print(start, end)
# 獲取正則表達式中第一個分組匹配到的內容的范圍
print(result.span(1))
b. start和end - 匹配到的字符串在原字符串中的開始下標和結束下標
print(result.start(),result.end())
c. group - 獲取匹配到的字符串(結果是字符串)
print(result.group()) # 獲取整個正則表達式匹配到的字符串
print(result.group(1)) # 獲取整個正則表達式第一個分組匹配到的字符串
print(result.group(2)) # 獲取整個正則表達式第二個分組匹配到的字符串
print(result.groups()) # 獲取整個正則表達式所有分組匹配到的字符串,返回結果是元組
d. string - 獲取原字符串
print(result.string)
4、 match(正則表達式,字符串) -->
讓字符串開頭和正則表達式進行匹配,匹配成功結果是匹配對象,否則是None
print(re.match(r'\D\d','s3432kj==3-2'))
5、 search(正則表達式,字符串) -->
在字符串中去匹配出第一個符合正則表達式的子串,匹配成功結果是匹配對象,否則是None
print(re.search(r'[\u4e00-\u9fa5]{3}','qwe你好呀qqqw今sad天asd天sad氣sad很sad好'))
6、 split(正則表達式,字符串) -->
將字符串按照滿足正則要求的子串進行切割(返回值是列表)
print(re.split(r'\d+','c1r2e3d4i45t'))
7、 sub(正則表達式,字符串1,字符串2) -->
將字符串2中能夠和正則表達式匹配的子串替換成字符串1,產生一個新的字符串
print(re.sub(r'\d+','*','qwq12er32'))
print(re.sub(r'qw|q','*','qwq12qwr32'))
8、 findall(正則表達式,字符串) -->
在字符串中獲取滿足正則表達式的所有的字符串(結果是列表)
# 注意:如果正則表達式中有分組,直接獲取到的是分組中;匹配到的內容;如果有多個分組,列表中的元素是元組
print(re.findall(r'\d+','sasd112ds2131sd213'))
print(re.findall(r'(\d+)([a-z])]','sasd112ds2131sd213'))
9、 finditer(正則表達式,字符串) -->
在字符串中獲取滿足正則表達式的所有子串(結果是迭代器,元素是匹配對象)
result = re.finditer(r'\d+','qwe3213dwe123')
print(result)
print(next(result))
10、 re.I --> 忽略大小寫
# 匹配的約束條件是放在函數的flags參數中的
print(re.fullmatch(r'[a-z]{2}','SA',re.I))
練習:驗證qq號,5-12位且首位不能為0,密碼是要求6-12位且由數字字母下劃線組成
re_str1 = r'[1-9][0-9]{4,11}'
re_str2 = r'[a-zA-Z\d_]{6,20}'
二 、 pygame的初步運用
import pygame
def base_game():
# 1、 初始化pygame,
pygame.init()
# 2、 創建游戲窗口
"""
set_mode(窗口大小) == set_mode((寬,高))
窗口大小對應的值是一個元組
"""
windows = pygame.display.set_mode((400, 600))
# 設置窗口標題
pygame.display.set_caption('游戲')
# 設置窗口背景顏色
"""
fill(顏色)
顏色是一個元組,元組有是三個int類型的元素,返回時0-255;分別R,G,B
計算機三原色:紅、綠、藍
紅色(255,0,0)
綠色(0,255,0)
藍色(0,0,255)
白色(255,255,255)
黑色(0,0,0)
"""
windows.fill((255,0,0))
pygame.display.flip()
# 3、 讓游戲保持運行狀態
while True:
# 4、 不斷檢測游戲過程中是否有事件的產生
#
for event in pygame.event.get():
# 只有當事件產生后才會進入for循環
if event.type == pygame.QUIT:
exit() # 退出
def main():
base_game()
if __name__ == '__main__':
main()
三、 顯示圖片
import pygame
def main():
# 1、 初始化pygame,
pygame.init()
# 2、 創建游戲窗口
"""
set_mode(窗口大小) == set_mode((寬,高))
窗口大小對應的值是一個元組
"""
windows = pygame.display.set_mode((400, 600))
pygame.display.set_caption('圖片')
windows.fill((255, 255, 255))
pygame.display.flip()
# 3、 添加固定顯示內容
"""
顯示圖片
a. 加載圖片
image.load(圖片地址) ---> 返回圖片對象(surface)
b. 將圖片添加(渲染)到窗口上
blit(渲染對象,坐標)
坐標:一個元組,兩個元素,分別是x坐標和y坐標
"""
image = pygame.image.load('images/W(C_`ECX5_AW7GYAF49ILR4.jpg')
windows.blit(image, (0, 0))
pygame.display.flip()
# 4、 操作圖片
"""
a.獲取圖片對象大小
surface類型有個對象方法:get_size() --- 返回的是元組
"""
# 獲取窗口大小,圖片大小
print(windows.get_size(), image.get_size())
width, height = image.get_size()
"""
b. 圖片縮放和旋轉(形變)
transform.scale(縮放對象,目標大小) --> 返回縮放后的新對象
transform.rotozoom(縮放對象,旋轉角度,縮放比例)
"""
# new_image = pygame.transform.scale(image,(50.50))
new_image = pygame.transform.rotozoom(image,0,0.8)
windows.blit(new_image,(10,250))
# 、 游戲循環
while True:
# 事件監測
for event in pygame.event.get():
if event.type == pygame.QUIT:
exit()
if __name__ == '__main__':
main()
四、 顯示字體
import pygame
def main():
pygame.init()
windows = pygame.display.set_mode((400, 600))
"""
1、 創建字體對象
a. 系統字體
font.SysFont(字體名,字體大小) --- 返回一個字體對象
b. 自定義字體
font,Font(字體文件路徑,字體大小)
"""
font = pygame.font.SysFont('images/font2.tff', 20)
"""
2、 根據字體創建文字對象
render(文字,True, color)
"""
text = font.render('hello world', True, (255,0.,0))
"""
3、 顯示文字
blit(渲染對象,坐標)
"""
windows.blit(text, (50, 50))
pygame.display.flip()
if __name__ == '__main__':
main()
五、 圖形化
import pygame
import math
def base_game():
pygame.init()
windows = pygame.display.set_mode((400, 600))
pygame.display.set_caption('游戲')
windows.fill((255, 0, 0))
"""
=========畫圖形=================
1.畫線
line(畫在哪個地方,線的顏色,起點,終點,線寬=1)
lines
"""
pygame.draw.line(windows, (0, 255, 0), (10, 10), (100, 100), 4)
point_list = [(200, 200), (250, 50), (300, 200), (150, 100), (350, 100)]
pygame.draw.lines(windows, (0, 255, 0), True, point_list)
# 3、 畫圓
"""
circle(畫在哪個地方,線的顏色,圓心,半徑,線寬=0)
"""
pygame.draw.circle(windows, (255, 255, 0), (200, 300), 100, 0)
# 4. 畫多邊形
# polygon()
pygame.draw.polygon(windows, (255, 255, 255), point_list, 0)
# 5. 畫弧線
# arc(畫在哪個地方,線的顏色,矩形,起始弧度,終止弧度,線寬)
pygame.draw.arc(windows, (0, 255, 255), (100, 470, 150, 200), math.pi / 4, math.pi / 4 * 3, 5)
# 6. 畫矩形
pygame.draw.rect(windows, (100, 20, 50), (200, 470, 100, 50), 2)
pygame.display.flip()
point_list = []
flag = False
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
exit()
if event.type == pygame.MOUSEBUTTONDOWN:
flag = True
point_list = []
elif event.type == pygame.MOUSEBUTTONUP:
flag = False
elif event.type == pygame.MOUSEMOTION:
point_list.append(event.pos)
if len(point_list) < 2:
break
if flag:
pygame.draw.lines(windows, (200, 0, 0), False, point_list, 3)
pygame.display.flip()
def main():
base_game()
if __name__ == '__main__':
main()