Skip to Content
Python 技術手冊 第三版
book

Python 技術手冊 第三版

by lex Martelli, Anna Martelli Ravenscroft, Steve Holden
January 2018
Intermediate to advanced content levelIntermediate to advanced
856 pages
17h 17m
Chinese
GoTop Information, Inc.
Content preview from Python 技術手冊 第三版
291
9
正規表達式
正規表達式(regular expressions)讓你指定模式字串(pattern strings),
並執行搜尋和替換。正規表達式並不容易精通,但它們可以是處理文字的
強大工具。Python 透過內建的
re
模組提供豐富的正規表達式功能。
正規表達式和 re 模組
一個
正規表達式
regular expression
RE)是從代表一個模式(pattern
的字串建置出來的。透過 RE 的功能性,你能以該模式(pattern)檢視任
何的字串,看看字串的哪個部分匹配(pattern)該模式,如果有的話。
re
模組供應 Python RE 功能性。
compile
函式會從一個模式字串和選擇
性的旗標(flags)建置出一個 RE 物件。RE 物件的方法會在字串中尋找
RE 的匹配,或進行替換。
re
模組也對外提供了與 RE 物件的方法等效的函
式,但以 RE 的模式字串作為第一引數。
RE 可能很難精通,而本書的目的並不是教導它們,我們只涵蓋你可以
Python 中使用 RE 的方式。對於 RE 的一般性介紹,我們推薦 Jeffrey
Friedl 所著的
Mastering Regular Expressions
O
Reilly)。 Friedl 的書完整
涵蓋了 RE 的入門指南和進階主題。RE 的許多教學指南和參考也可以在
線上找到,包括線上文件中一個很棒的詳盡教程(
https://docs.python.org/3/
howto/regex.html
)。像是Pythex
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

測試驅動開發|使用 Python

測試驅動開發|使用 Python

Harry J.W. Percival
Linux 内核观测技术BPF

Linux 内核观测技术BPF

David Calavera, Lorenzo Fontana

Publisher Resources

ISBN: 9789864766819