《Python Cookbook 3rd》笔记(2.4):字符串匹配和搜索
字符串匹配和搜索
問題
你想匹配或者搜索特定模式的文本
解法
如果你想匹配的是字面字符串,那么你通常只需要調用基本字符串方法就行,比如 str.find() , str.endswith() , str.startswith() 或者類似的方法:
>>> text = 'yeah, but no, but yeah, but no, but yeah' >>> # Exact match >>> text == 'yeah' False >>> # Match at start or end >>> text.startswith('yeah') True >>> text.endswith('no') False >>> # Search for the location of the first occurrence >>> text.find('no') 10 >>>對于復雜的匹配需要使用正則表達式和 re 模塊。為了解釋正則表達式的基本原理,假設你想匹配數字格式的日期字符串比如 11/27/2012 ,你可以這樣做:
>>> text1 = '11/27/2012' >>> text2 = 'Nov 27, 2012' >>> >>> import re >>> # Simple matching: \d+ means match one or more digits >>> if re.match(r'\d+/\d+/\d+', text1): ... print('yes') ... else: ... print('no') ... yes >>> if re.match(r'\d+/\d+/\d+', text2): ... print('yes') ... else: ... print('no') ... no >>>如果你想使用同一個模式去做多次匹配,你應該先將模式字符串預編譯為模式對象。比如:
>>> datepat = re.compile(r'\d+/\d+/\d+') >>> if datepat.match(text1): ... print('yes') ... else: ... print('no') ... yes >>> if datepat.match(text2): ... print('yes') ... else: ... print('no') ... no >>>match() 總是從字符串開始去匹配,如果你想查找字符串任意部分的模式出現位置,使用 findall() 方法去代替。比如:
>>> text = 'Today is 11/27/2012. PyCon starts 3/13/2013.' >>> datepat.findall(text) ['11/27/2012', '3/13/2013'] >>>在定義正則式的時候,通常會利用括號去捕獲分組。比如:
>>> datepat = re.compile(r'(\d+)/(\d+)/(\d+)') >>>捕獲分組可以使得后面的處理更加簡單,因為可以分別將每個組的內容提取出來。比如:
>>> m = datepat.match('11/27/2012') >>> m <_sre.SRE_Match object at 0x1005d2750> >>> # Extract the contents of each group >>> m.group(0) '11/27/2012' >>> m.group(1) '11' >>> m.group(2) '27' >>> m.group(3) '2012' >>> m.groups() ('11', '27', '2012') >>> month, day, year = m.groups() >>> >>> # Find all matches (notice splitting into tuples) >>> text 'Today is 11/27/2012. PyCon starts 3/13/2013.' >>> datepat.findall(text) [('11', '27', '2012'), ('3', '13', '2013')] >>> for month, day, year in datepat.findall(text): ... print('{}-{}-{}'.format(year, month, day)) ... 2012-11-27 2013-3-13 >>>findall() 方法會搜索文本并以列表形式返回所有的匹配。如果你想以迭代方式返回匹配,可以使用 finditer() 方法來代替,比如:
>>> for m in datepat.finditer(text): ... print(m.groups()) ... ('11', '27', '2012') ('3', '13', '2013') >>>討論
本文闡述了使用 re模塊進行匹配和搜索文本的最基本方法。核心步驟就是先使用 re.compile() 編譯正則表達式字符串,然后使用 match() , findall() 或者 finditer() 等方法。
當寫正則式字符串的時候,相對普遍的做法是使用原始字符串比如 r’(\d+)/(\d
+)/(\d+)’ 。這種字符串將不去解析反斜杠,這在正則表達式中是很有用的。如果不這樣做的話,你必須使用兩個反斜杠,類似 ‘(\d+)/(\d+)/(\d+)’ 。
如果你想精確匹配,確保你的正則表達式以 $ 結尾,就像這么這樣:
>>> datepat = re.compile(r'(\d+)/(\d+)/(\d+)$') >>> datepat.match('11/27/2012abcdef') >>> datepat.match('11/27/2012') <_sre.SRE_Match object at 0x1005d2750> >>>最后,如果你僅僅是做一次簡單的文本匹配/搜索操作的話,可以略過編譯部分,直接使用 re 模塊級別的函數。比如:
>>> re.findall(r'(\d+)/(\d+)/(\d+)', text) [('11', '27', '2012'), ('3', '13', '2013')] >>>但是需要注意的是,如果你打算做大量的匹配和搜索操作的話,最好先編譯正則表達式,然后再重復使用它。模塊級別的函數會將最近編譯過的模式緩存起來,因此并不會消耗太多的性能,但是如果使用預編譯模式的話,你將會減少查找和一些額外的處理損耗。
總結
以上是生活随笔為你收集整理的《Python Cookbook 3rd》笔记(2.4):字符串匹配和搜索的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 机器学习知识总结系列- 模型评估(1-2
- 下一篇: 《Python Cookbook 3rd