python正则表达式知识点
生活随笔
收集整理的這篇文章主要介紹了
python正则表达式知识点
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
import re
str1="hello world my11 phone1 number is 123243331124" \"I am 18 years old"
#re.findall目的就是匹配字符串當中所有滿足條件的字符
result=re.findall(r"l",str1)#原樣匹配,匹配字符原樣,通常結合其他匹配使用,匹配l開頭的單詞
result=re.findall(r".",str1)#匹配所有非換行的字符,幾個點就是幾個字符
result=re.findall(r"\d\d",str1)#匹配所有字符串中的數字
result=re.findall(r"\d",str1)#大小寫通常是相反的
result=re.findall(r"\D",str1)#匹配所有字符串當中的1非數字
result=re.findall(r"\w",str1)#\w匹配字符串當中的所有字母、數字、下劃線(漢字屬于字母)
result=re.findall(r"\W",str1)#\W匹配字符串中的所有非字母、非數字、非下劃線
result=re.findall(r'[hello]',str1)#匹配括號當中的任意元素
result=re.findall(r"[a-zA-A0-9]",str1)#匹配當中的范圍
result=re.findall(r"[^lo]",str1)#匹配除了括號當中元素之外的任意元素(括號當中的所有元素)
result=re.findall(r"[hello]",str1)#匹配括號中的任意元素
result=re.findall(r"he|ll|ph",str1)#匹配|兩邊的任意一邊
#()組匹配 ()是組匹配
result=re.findall(r"\w\d",str1)#一個字母數字或下劃線和一個數字
result=re.findall(r"\w(\d)",str1)#組匹配,會將非組內匹配,作為匹配的條件進行匹配
#如果是一個字母數字或下劃線和一個數字,輸出數字
#(?P<num>\d)\w(?P=num)是命名組匹配
result=re.findall(r"(?P<num>\d)\w(?P=num)",str1)#匹配一個和前面相同的數字,中間有一個
#字母數字或下劃線
result=re.findall(r"\d*",str1)#*匹配0到多個,/d數字
result=re.findall(r"\d+",str1)#+匹配1到多個,/d數字
result=re.findall(r"\d{3}",str1)#匹配3個
result=re.findall(r"\d{3,5}",str1)#匹配3到5個1
h5="""
<img src="1.jpg"><img src="2.jpg"><img src="3.jpg">"""result=re.findall(r'<img src="(.*)"',h5)#貪婪匹配
result=re.findall(r'<img src="(.*?)"',h5)#反貪婪
str2="hello world hi man"
result=re.findall(r'^h',str2)#字符串的開頭,如果第一個元素是選擇的元素,則返回該元素
#否則返回空
result=re.findall(r'n$',str2)#字符串的結尾
#re.search和re.match都是匹配一次指定的字符,re.search是從字符內部開始匹配,
#而match是從開頭匹配,也就是說,如果開頭沒有,search會向后接著匹配,match會返回None
result=re.search(r'n',str2)#從1字符串內部進行匹配,匹配成功一次1返回,沒有返回None
# print(result)
# print(result.group())
# print(result.groups())
# #search搜查 match匹配
# result=re.match(r'h',str2)#從字符串開頭進行匹配,匹配成功一次返回,沒有返回None
# #match只匹配開頭第一個
# print(result)
# print(result.group())
# print(result.groups())#按照組返回一個匹配結果的元組
str3="""
hello WORLD 李易松
HI man"""
result=re.findall(r".",str3,re.S)#.忽略換行進行匹配,通常用于完整的html源碼
result=re.findall(r".",str3)#re.S將忽略的換行符重新匹配
result=re.findall(r'[a-z]',str3,re.I)#忽略大小寫匹配
#re.I是大寫的i將大小寫都匹配出來
result=re.findall(r"\w",str3)#\w匹配字符串當中的所有字母、數字、下劃線(漢字屬于字母)
print(result)
總結
以上是生活随笔為你收集整理的python正则表达式知识点的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Django输入日期返回第几天time
- 下一篇: Django远端访问