python 核心程式設計》第一章 正規表示式

2021-08-04 11:11:51 字數 4778 閱讀 9710

import re
m = re.match('foo|bar','foo')
print(m.group())
foo
type(m.group())
str
m.group() 返回匹配的字串

pattern = 'foo|bar'

m = re.match(pattern,'foobarfooooooook')

print(m.group())

foo
re.match() 返回從支付串頭部開始的匹配,若沒有則返回none,若有返回匹配字元

m = re.match(pattern,'ofoo')

m is

none

true
m = re.search(pattern,'ofoo')

if m is

notnone:

print(m.group())

foo
search 找第一次匹配的字串

anyend = '.end'

m = re.match(anyend,'okend')

if m is

notnone:

print(m.group())

else :

print('none')

tmp = re.match(anyend,'oend')

print(tmp.group())

none

oend

『.』匹配單個字元,除了』\n』

m = re.match(anyend,'\nend')

if m is

notnone:

print(m.group())

else:

print('can\'t mtach "\\n"')#加'\n'表示本源字元

can't mtach "\n"
建立字符集,相當於邏輯或的功能,例如[abcd][defg]表示匹配第乙個字元為』abcd』第二個字元為』defg』

pat = '[abcd][defg]'

m = re.match(pat,'adefg')

if m is

notnone:

print(m.group())

else:

print('can\'t match ')

#防止多次呼叫書寫,寫作函式

defprint_match

(m):

if m is

notnone:

print(m.group())

else:

print('can\'t match')

ad
字符集可以用作限定範圍的匹配

eg:

* [a-z]匹配小寫字母

* [0-9]匹配數字

* [^0-9]不匹配數字

eg:

* [0-9]匹配9次數字

*特殊字符集的大寫表示不匹配

eg:

* \d 表示不匹配數字相當於[^0-9]

就相當於四則運算中的結合,將其作為乙個整體看待

eg:

* \d+(.\d+)? 十進位制小數

將其分組可用m.group(1)呼叫第乙個子組當然group(2)呼叫第二個子組..m.groups()返回匹配的所有元組的字組

pat = '\w\w\w-\d'

m = re.match(pat,'abc-123')

print_match(m)

abc-123
pat = '(\w)-(\d)'

#分兩個子組

m = re.match(pat,'abx-123')

print_match(m)

print('m.group1',m.group(1))

print('m.group2',m.group(2))

print('m.groups',m.groups())

abx-123

m.group1 abx

m.group2 123

m.groups ('abx', '123')

eg:

pat = r'\bthe'

#原生字元避免轉義,因為'\b'表示空格

m = re.search(pat,'othe')

print_match(m)

can't match
pat = '\bthe'

m = re.search(pat , 'othe')

print_match(m)

the
pat = r'(th\w+) and (th\w+)'

s = 'this and that'

print('findall')

print(re.findall(pat,s,re.i)) #標記re.i表示忽略大小寫的匹配

print('finditer')

[x for g in re.finditer(pat,s,re.i) for x in g.groups()]

findall

[('this', 'that')]

finditer

['this', 'that']

sub(patern,repl,string,count=0)

用repl替換pattern在字串中出現的位置,count為0表示全部替換

subn還返回乙個替換總數

pat = 'x'

s = 'mr zou'

print(re.sub(pat,s,'att:x\n\ndear x\n\n'))

print(re.subn(pat,s,'att:x\n\ndear x\n\n'))

att:mr zou

dear mr zou

('att:mr zou\n\ndear mr zou\n\n', 2)

#group除了匹配分組編號外還可以使用\n其中n是分組編號

#日期格式替換

re.sub(r'(\d)/(\d)/(\d|\d)',r'\2/\1/\3','2/20/91')

'20/2/91'
eg:

* (?i)表示對後面的字串忽略大小寫注意必須放在匹配模式開頭

* (?s) 點號全匹配

re.findall(r'(?i)yes','oyesok,oyesok,yes')
['yes', 'yes', 'yes']
對正規表示式分組,但不儲存結果用作後續檢索或操作

re.findall(r'http://(?:\w+\.)*(\w+\.com)','')#只返回分組的東西
['code.com']
用name替代分組編號

search 返回乙個字典,name對應乙個key

pat = r'\((?p\d)\) (?p\d)-(?:\d)'

re.search(pat,'(800) 555-1212')

<_sre.sre_match object; span=(0, 14), match='(800) 555-1212'>
對應的可用反斜槓g進行解析

\g < name >

re.sub(pat,'(\g) \g-x','(800) 555-1212')
'(800) 555-x'
pat = r'^\s+(?!noreply|postmaster)(\w+)'

#(\w+)匹配此分組不為noreply or postmaster

re.findall(pat,'''

[email protected]

[email protected]

[email protected]

''',re.m)

['sales', '12345678']
(?(id/name)y|n) 若分組id(or name)存在就與y匹配否則與n匹配|n是可選項

#匹配只由x與y的交錯項組成的字串

bool(re.search(r'(?:(x)|y)(?(1)y|x)','xyxyxy'))

true

python第一章筆記 第一章 基礎

參與除法的兩個數中有乙個數為浮點數,結果也為浮點數 如 1.0 2,1 2.0,1.0 2.0 python print 1.0 2 結果 0.5 print 1 2.0 結果 0.5 print 1.0 2.0 結果 0.5 整數 整數,計算結果的小數部分被截除,只保留整數部分 不會四捨五入 如 ...

windows 核心程式設計 第一章學習筆記

windows 核心程式設計第五版 第一章 錯誤處理 1.呼叫系統api時候仔細檢視msdn返回值含義。2.函式呼叫失敗後,可以立即呼叫 getlasterror來檢視錯誤碼。3.可以利用visual studio 自帶工具 error lookup 來檢視錯誤碼對應的含義。4.在除錯的時候在wat...

python核心程式設計第3版第一章習題

個人練習,不斷更新中,歡迎提意見 1 1 import re res re.findall bh aui t bat but hat hit hut print res 1 2 import re 注意單詞中可包括 號 res re.findall r a za z a za z junpeng z...