日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

《python核心编程》读书笔记--第15章 正则表达式

發布時間:2025/1/21 python 83 豆豆
生活随笔 收集整理的這篇文章主要介紹了 《python核心编程》读书笔记--第15章 正则表达式 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

15.1引言與動機

處理文本和數據是一件大事。正則表達式(RE)為高級文本匹配模式,為搜索-替換等功能提供了基礎。RE是由一些字符和特殊符號組成的字符串,它們描述了這些字符和字符串的某種重復方式,因此能按某種模式匹配一個有相似特征的字符串的集合,也就是說,一個只能匹配一個字符串的RE是無聊的。

Python通過標準庫的re模塊支持正則表達式。

15.2正則表達式使用的特殊符號和字符

正則表達式中常見的字符列表。包括|、.、等,這個方面已經有很多講解。比如這篇:http://blog.csdn.net/pleasecallmewhy/article/details/8929576(引用感謝)。

15.3正則表達式和Python語言

這一節將python中的re模塊。主要講match、search、compile等函數。

#-*- coding:utf-8 -*- import re#下面看一下match和group的基本用法 m = re.match('foo','foo') if m is not None:print m.group()m = re.match('foo','car') if m is not None:print m.group()m = re.match('foo','foo on the table') print m.group() print '-'*50 #下面是search和match的區別,search會從任意的地方開始搜索匹配模式 m = re.match('foo','seafood') if m is not None:print m.group() #匹配失敗 m = re.search('foo','seafood') if m is not None:print m.group() #匹配成功 print '-'*50 bt = 'bat|bet|bit' m = re.match(bt,'bat') if m is not None:print m.group() m = re.match(bt,'He bit me') if m is not None:print m.group() #匹配不成功 m = re.search(bt,'He bit me') if m is not None:print m.group() #匹配成功 print '-'*50 #下面將要說明句點不能匹配換行符 anyend = '.end' m = re.match(anyend,'bend') if m is not None:print m.group() #匹配成功 m = re.match(anyend,'\nend') if m is not None:print m.group() #匹配不成功 m = re.search(anyend,'The end') if m is not None:print m.group() print '-'*50 #用轉義字符表示句點 p1 = '3.14' p2 = '3\.14' print re.match(p1,'3.14').group() #成功,注意這里的.也屬于‘任意字符’ print re.match(p1,'3014').group() #成功 print re.match(p2,'3.14').group() #成功 print re.match(p2,'3014').group() #出現錯誤>>> foo foo -------------------------------------------------- foo -------------------------------------------------- bat bit -------------------------------------------------- Traceback (most recent call last):bendend -------------------------------------------------- 3.14 3014 3.14File "E:\NUT\PY\pycore\chapter15.py", line 45, in <module>print re.match(p2,'3014').group() AttributeError: 'NoneType' object has no attribute 'group' [Finished in 0.1s with exit code 1] #-*- coding:utf-8 -*- import re#下面是創建字符集合 []和|的區別 m = re.match('[cr][23][dp][o2]','c3po') print m.group() #成功匹配 print re.match('[cr][23][dp][o2]','c2do').group() #成功匹配 m = re.match('r2d2|c3po','c2do') #并不成功 if m is not None:print m.group() print '-'*50 #重復、特殊字符和子組 #正則表達式最常見的情況包括特殊字符的使用,正則表達式模式的重復出現,以及使用圓括號對匹配模式進行分組和提取操作 patt = '\w+@(\w+\.)?\w+\.com' print re.match(patt,'ored@xxx.com').group() print re.match(patt,'ored@www.xxx.com').group() print '-'*50 #下面看一下子組 m = re.match('(\w\w\w)-(\d\d\d)','abc-123') print m.group() #group返回所有匹配的內容 print m.group(1) print m.group(2) print m.groups() #groups返回所有子組組成的元組 #下面用幾個例子展示group和groups的不同 print '-'*50 m = re.match('ab','ab') #沒有子組 print m.group() #返回完全匹配 print m.groups() #返回空 m = re.match('(ab)','ab') #這樣的形式是有一個子組 print m.group() print m.groups() print '-'*50 m = re.match('(a)(b)','ab') print m.group() #注意這里的機制是整體匹配 print m.groups() m = re.match('(a(b))','ab') print m.groups() #首先匹配外層,再匹配內層 >>> c3po c2do -------------------------------------------------- ored@xxx.com ored@www.xxx.com -------------------------------------------------- abc-123 abc 123 ('abc', '123') -------------------------------------------------- ab () ab ('ab',) -------------------------------------------------- ab ('a', 'b') ('ab', 'b') [Finished in 0.1s]

轉載于:https://www.cnblogs.com/batteryhp/p/5188376.html

總結

以上是生活随笔為你收集整理的《python核心编程》读书笔记--第15章 正则表达式的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。