Python3的re模塊的使用

import re

# 2元字符 . ^ $ * + ? { } [ ] | ( ) \
# 作用:匹配字符串

s = 'hello world'
# 返回開始位置 下標
print(s.find('llo'))

# 找到 並替換
print(s.replace('ll', 'xx'))

# . 代指一位字符,代指所有字符 除了換行符 \n
ci = re.findall('w\w{2}l', 'hello world')
print(ci)

# ^ 只會在開始時進行匹配
ci = re.findall('^h..l', 'hello world')
print(ci)

# $ 只會在最後時進行匹配
ci = re.findall('o..d$', 'hello world')
print(ci)

# * 重複匹配 從h開始到o(最後一個)中間可以任意字符
ci = re.findall('h.*o', 'hello world')
print(ci)

# + 至少需要一個 只對前面第一個字符有作用
ci = re.findall('a+b', 'aaaaabcccdddabbbbb')
print(ci)

# ? 只能取0或1次 只對前面第一個字符有作用 出現a 0次或1次
ci = re.findall('a?b', 'abcdbb,acb,addb')
print(ci)

# {n}  出現n次 只對前面第一個字符有作用
ci = re.findall('a{2}b', 'abcdbb,acb,aabddb')
print(ci)

# +等價於 {1,正無窮} 也等價於 {1, }
# * 等價於 {0,正無窮}    ? 等價於{0,1}
# {m,n}  出現m到n次 只對前面第一個字符有作用
ci = re.findall('a{1,3}b', 'abcdbb,acb,aabddb')
print(ci)

# x[m,n]y 字符集 或的關係 xmy或 xny
ci = re.findall('a[c,d]b', 'abcdbb,acb,aabddb')
print(ci)

# [] 取消元字符的特殊功能 (三個例外:\,^,-)
ci = re.findall('[w,,,*]', 'abwdbb,,acb,aa*ddb')
print(ci)

ci = re.findall('[1-9,a-z,A-Z]', '12asAS')
print(ci)

# [^] 就變成了 取反操作
ci = re.findall('[^(a-z)]', '12aimsAS')
print(ci)

# ^ 是取[] 內的所有非
ci = re.findall('[^12,3,4]', '1234567891')
print(ci)

# \ 反斜槓
# \ 1.反斜槓後邊跟元字符去除特殊功能 2.反斜槓後邊跟普通字符實現特殊功能
# \w	匹配 數字 字母 下劃線
# \W	匹配 非數字 非字母 非下劃線
# \s	匹配 任意空白字符,等價於 [\t\n\r\f]。
# \S	匹配 任意非空字符
# \d	匹配 任意數字,等價於 [0-9]。
# \D	匹配 任意非數字
# \b	匹配一個單詞邊界,也就是指單詞和空格間的位置。

ci = re.findall('\w', '19 az__')
print(ci)
ci = re.findall('\W', '19 az__')
print(ci)
ci = re.findall('\s', '19 az__')
print(ci)
ci = re.findall('\S', '19 az__')
print(ci)
ci = re.findall('\d', '19 az__')
print(ci)
ci = re.findall('\D', '19 az__')
print(ci)
ci = re.findall(r'er\b', '19 er az__over ')
print(ci)

# 符合的第一個結果
print(re.search('as', 'hgahashjjas'))

# 想要匹配 \ 沒找到 加r 表示匹配原生字符 不進行轉義
print(re.findall(r'\\', 'adhfD\c'))

print(re.findall(r'\\', 'adhfD\c'))

# | 是或的意思 會先取第一個匹配結果
print(re.search('(as)+', 'adsdasas').group())
print(re.search('(as)|3', 'as3').group())

# ?P<name> 固定格式 給分組起一個名字 爲name 規則爲:\d{3}
name = re.search('(?P<id>\d{3})', 'wwwwddash34ttt123/ooo')
print(name.group('id'))
# 連續分成兩個組 分別起名 匹配規則內的 / 也是匹配規則
name = re.search('(?P<num>\d{3})/(?P<letter>\w{3})', 'wwwwddash34ttt123/abc')
print(name.group())
print(name.group('num'))
print(name.group('letter'))

# 正則表達式的方法
# 1. findall():所有結果都返回到一個列表裏
# 2. search():返回一個對象(object),對象可以調用group()返回結果
# 3. match():只在字符串開始匹配,也返回匹配到的第一個對象,對象可以調用group()
# 4. split() 如果在邊緣 則會有空字符
print(re.split('k', 'ashdjakah'))
print(re.split('[h,k]', 'ashdjakah'))
# 5. sub() 替換函數,使用方式參看下面
print(re.sub('j..', 's..b', 'hello joe world'))
# 6. compile() # 記爲一個正則表達式 下次可以直接使用
obj = re.compile('\.com')
print(obj.findall('ahfjhj.com'))

要點:

  •  find() 返回第一個匹配位置下標
  • replace() 找到 並替換
  • 2元字符 . ^ $ * + ? { } [ ] | ( ) \
  • . 代指一個字符,除了換行符\n 以外的其他所有字符
  • ^ 只會在開始位置進行匹配
  • $ 只會在末尾位置進行匹配
  • * 可重複匹配多個字符:h.*o  可以匹配到hello
  • + 至少匹配到一個: a+b 可以匹配到 aaaab 或 ab
  • ? 只能匹配到0個或1個: a?b 可以匹配到 b 或 ab
  • {n} 匹配到n次:a{3}b 可以匹配到 aaab
  • 小結:1. + 等價 {1,正無窮};2. * 等價 {0,正無窮};3. ?等價{0,1} 
  • 字符集[ ],括號內是或的關係:a[m,n]b 可以匹配到 amb 或 anb
  • [ ]  還有取消元字符的特殊功能(三個例外:\ , ^ , -)
  • [^] 取反操作:[^(a-z)] 表示匹配除了小寫字母以外的所有字符
  • \ 反斜槓:\w 匹配數字,字母下劃線 。\W 匹配非數字,非字母。非下劃線。\s 匹配任意空白字符 等價[\t \n \r \f]。 \S匹配任意非空字符。\d 匹配任意數字,等價[0-9]。\D 匹配非數字。 \b 匹配單詞邊界:er\b 可以匹配‘19 er az_over’ 中的 一個'er'。
  • \ 如果想匹配反斜槓 \ 就在前面加 r  表示不進行轉義:r'\\' 可以匹配到 \ 
  • | 表示或的意思,但是如果第一個匹配,就匹配第一個,不在匹配第二個: '(as)|3' 匹配 ‘as3’ 匹配到as  而不會是3。
  • ?P<name>fmt/?P<name>fmt/?P<name>fmt ... 用/ 分隔開 ?P<name>fmt 中的name是別名,fmt 是匹配規則。可以通過group('name') 獲取到匹配結果。
  • findall() 所有結果返回到一個列表
  • search() 返回一個對象,可以調用group()返回結果
  • match() 在字符串開始匹配,返回匹配的第一個對象,可以調用group()返回結果
  • split(‘w’,'awawaw')  以 w 分開awawaw。如果w在邊緣,則會有空字符串 '空' 。
  • sub(‘w’,'a','awawaw')  表示以w替換a,最後字符串變成,wwwwww
  • compile() 記爲一個匹配規則。my_re=compile('\.com') ,匹配'.com' 記爲my_re。my_re.findall('www.baidu.com') 返回.com

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章