-
group(0) 就等于group()是匹配的整體結(jié)果,第一個(gè)小括號(hào)里面的匹配是從group(1)開始算起的 -
\w匹配 匹配非特殊字符,即a-z、A-Z、0-9、_、漢字,但是.是匹配任意字符,除了換行符,當(dāng)re.DOTALL標(biāo)記被指定時(shí),則可以匹配包括換行符的任意字符,因?yàn)槲覀兊钠ヅ渲幸ヅ?code>.所以不使用\w
with open(FILEPATH,'r') as file:
for index,line in enumerate(file):
lineSearch=re.search("/a/\[/r/(.+)/,/c/en/(.+)/.*,/c/en/(.+)/\]",line)
if lineSearch!=None and lineSearch.group(1)!=None and lineSearch.group(2)!=None:
print(lineSearch.group(2),' ',lineSearch.group(3))
使用懶惰匹配+?或者'*?'目的是匹配到第一個(gè)字符就終止,比如下面例子中因?yàn)閼卸杵ヅ浜竺媸且粋€(gè)/所以他匹配到第一個(gè)/就停止了,所以是懶惰匹配。如果不加?是貪婪匹配他會(huì)匹
/(.+?)/.*
re.DOTALL的用法
- 當(dāng)我們需要希望使用
.來(lái)匹配任意字符時(shí),但是.是不能匹配換行符'\n'的,因此可以設(shè)置flag=re.DOTALL來(lái)匹配換行符.
import re
comment = re.compile(r'/\*(.*?)\*/') # 匹配C的注釋
text1 = '/* this is a comment */'
text2 = """/*this is a
multiline comment */"""
comment.findall(text1)
Out[4]: [' this is a comment ']
comment.findall(text2) # 由于text2文本換行了,沒(méi)匹配到
Out[5]: []
- 解決方法一:添加對(duì)換行符的支持,
(?:.|\n)指定了一個(gè)非捕獲組(即,這個(gè)組只做匹配但不捕獲結(jié)果,也不會(huì)分配組號(hào))
comment = re.compile(r'\*((?:.|\n)*?)\*/')
comment.findall(text2)
Out[7]: ['this is a \n multiline comment ']
- 解決方法二:設(shè)置
flag=re.DOTALL使得.可以用來(lái)匹配換行符
comment = re.compile(r'/\*(.*?)\*/', flags=re.DOTALL)
comment.findall(text2)
Out[10]: ['this is a \n multiline comment ']
?: ?= ?<
- 前瞻: 查找
exp2前面的exp1
exp1(?=exp2)
- 后顧:查找
exp2后面的exp1
(?<=exp2)exp1
- 負(fù)前瞻:查找后面不是
exp2的exp1
exp1(?!exp2)
- 負(fù)后顧:查找前面不是
exp2的exp1
(?<!=exp2)exp1
- 例子:這個(gè)應(yīng)用經(jīng)常見(jiàn),比如我們要匹配被這個(gè)格式保衛(wèi)的目標(biāo)字符串
<start>字符串<end>
>>> m = re.search(r'(?<=-)\w+', 'spam-egg')
>>> m.group(0)
'egg'
-
?:是匹配但是不捕獲結(jié)果
一些函數(shù)的用法
-
re.search()因?yàn)榭赡芷ヅ洳坏?,所以一般要判斷一?/li>
str = 'an example word:cat!!'
match = re.search(r'word:\w\w\w', str)
if match:
print 'found', match.group() ## 'found word:cat'
else:
print 'did not find'
>>> found word:cat
-
[]類似于or
match = re.search(r'[\w.-]+@[\w.-]+',string)
if match:
print match.group()
>>> alice-b@gmail.com
-
findall:返回的是a list of tuples
str = 'purple alice@google.com, blah monkey bob@abc.com blah dishwasher'
tuples = re.findall(r'([\w\.-]+)@([\w\.-]+)', str)
print tuples # [('alice', 'google.com'), ('bob', 'abc.com')]
for tuple in tuples:
print tuple[0] # username
print tuple[1] # host
>>> [('alice', 'google.com'), ('bob', 'abc.com')]
alice
google.com
bob
abc.com
-
re.sub:re.sub(pat, replacement, str)在str里尋找和pat匹配的字符串,然后用replacement替換。replacement可以包含\1或者\2來(lái)代替相應(yīng)的group,然后實(shí)現(xiàn)局部替換。
# replace hostname
str = 'alice@google.com, and bob@abc.com'
#returns new string with all replacements,
# \1 is group(1), \2 group(2) in the replacement
print re.sub(r'([\w\.-]+)@([\w\.-]+)', r'\1@yo-yo-dyne.com', str)
>>> alice@yo-yo-dyne.com, and bob@yo-yo-dyne.com
速記表
-
元字符不匹配自己,若想匹配自己要加上轉(zhuǎn)義字符。
正則表達(dá)式字符串前面的
-
Python中字符串前面加上r表示原生字符串。
與大多數(shù)編程語(yǔ)言相同,正則表達(dá)式里使用"\"作為[轉(zhuǎn)義字符],這就可能造成反斜杠困擾。假如你需要匹配文本中的字符"\",那么使用編程語(yǔ)言表示的正則表達(dá)式里將需要4個(gè)反斜杠"\\\\":前兩個(gè)和后兩個(gè)分別用于在編程語(yǔ)言里轉(zhuǎn)義成反斜杠,轉(zhuǎn)換成兩個(gè)反斜杠后再在正則表達(dá)式里轉(zhuǎn)義成一個(gè)反斜杠。Python里的原生字符串很好地解決了這個(gè)問(wèn)題,匹配一個(gè)數(shù)字的"\\d"可以寫成r"\d"。有了原生字符串,你再也不用擔(dān)心是不是漏寫了反斜杠,寫出來(lái)的表達(dá)式也更直觀。
