1、查找文本中的模式
search()函數取模式和要掃描的文本作爲輸入,如果找到這個模式則返回一個Match對象,如果沒找到,search()將返回None
- #!/usr/bin/python
- import re
- pattern = 'this'
- text = 'Does this text match the pattern?'
- match = re.search(pattern, text)
- s = match.start()
- e = match.end()
- print 'Found "%s"\nin "%s"\nfrom %d to %d ("%s")' % \
- (match.re.pattern, match.string, s, e, text[s:e])
結果:
Found "this"
in "Does this text match the pattern?"
from 5 to 9 ("this")
2、編譯表達式
re包含一些模塊級函數,用於處理作爲文本字符串的正則表達式,不過對於程序頻繁使用的表達式,編譯這些表達式會更爲高效。compile()函數會把一個表達式字符串轉換爲一個RegexObject。
- #!/usr/bin/python
- import re
- regexes = [ re.compile(p)
- for p in ['this', 'that']
- ]
- text = 'Does this text match the pattern?'
- print 'Text: %r\n' % text
- for regex in regexes:
- print 'Seeking "%s" ->' % regex.pattern,
- if regex.search(text):
- print 'match!'
- else:
- print 'no match'
結果:
Text: 'Does this text match the pattern?'
Seeking "this" -> match!
Seeking "that" -> no match
3、多重匹配
findall()函數返回輸入中與模式匹配而不重疊的所有子串。
- #!/usr/bin/python
- import re
- text = 'abbaaabbbbaaaaa'
- pattern = 'ab'
- for match in re.findall(pattern, text):
- print 'Found "%s"' % match
結果:
Found "ab"
Found "ab"