Python正则表达式操作指南
原文出處:http://www.amk.ca/python/howto/regex/
原文作者:A.M. Kuchling (amk@amk.ca)
授權許可:創作共用協議
翻譯人員:FireHare
校對人員:Leal
適用版本:Python 1.5 及后續版本
文章狀態:校對階段
Abstract(摘要)
This document is an introductory tutorial to using regular expressions in Python with the re module. It provides a gentler introduction than the corresponding section in the Library Reference.
本文是通過Python的 re 模塊來使用正則表達式的一個入門教程,和庫參考手冊的對應章節相比,更為淺顯易懂、循序漸進。
This document is available from http://www.amk.ca/python/howto .
本文可以從 http://www.amk.ca/python/howto 捕獲
Contents(目錄)
?
目錄
?
Introduction(簡介)
The re module was added in Python 1.5, and provides Perl-style regular expression patterns. Earlier versions of Python came with the regex module, which provides Emacs-style patterns. Emacs-style patterns are slightly less readable and don't provide as many features, so there's not much reason to use the regex module when writing new code, though you might encounter old code that uses it.
Python 自1.5版本起增加了re 模塊,它提供 Perl 風格的正則表達式模式。Python 1.5之前版本則是通過 regex 模塊提供 Emecs 風格的模式。Emacs 風格模式可讀性稍差些,而且功能也不強,因此編寫新代碼時盡量不要再使用 regex 模塊,當然偶爾你還是可能在老代碼里發現其蹤影。
Regular expressions (or REs) are essentially a tiny, highly specialized programming language embedded inside Python and made available through the re module. Using this little language, you specify the rules for the set of possible strings that you want to match; this set might contain English sentences, or e-mail addresses, or TeX commands, or anything you like. You can then ask questions such as "Does this string match the pattern?", or "Is there a match for the pattern anywhere in this string?". You can also use REs to modify a string or to split it apart in various ways.
就其本質而言,正則表達式(或 RE)是一種小型的、高度專業化的編程語言,(在Python中)它內嵌在Python中,并通過 re 模塊實現。使用這個小型語言,你可以為想要匹配的相應字符串集指定規則;該字符串集可能包含英文語句、e-mail地址、TeX命令或任何你想搞定的東西。然后你可以問諸如“這個字符串匹配該模式嗎?”或“在這個字符串中是否有部分匹配該模式呢?”。你也可以使用 RE 以各種方式來修改或分割字符串。
Regular expression patterns are compiled into a series of bytecodes which are then executed by a matching engine written in C. For advanced use, it may be necessary to pay careful attention to how the engine will execute a given RE, and write the RE in a certain way in order to produce bytecode that runs faster. Optimization isn't covered in this document, because it requires that you have a good understanding of the matching engine's internals.
正則表達式模式被編譯成一系列的字節碼,然后由用 C 編寫的匹配引擎執行。在高級用法中,也許還要仔細留意引擎是如何執行給定 RE ,如何以特定方式編寫 RE 以令生產的字節碼運行速度更快。本文并不涉及優化,因為那要求你已充分掌握了匹配引擎的內部機制。
The regular expression language is relatively small and restricted, so not all possible string processing tasks can be done using regular expressions. There are also tasks that can be done with regular expressions, but the expressions turn out to be very complicated. In these cases, you may be better off writing Python code to do the processing; while Python code will be slower than an elaborate regular expression, it will also probably be more understandable.
正則表達式語言相對小型和受限(功能有限),因此并非所有字符串處理都能用正則表達式完成。當然也有些任務可以用正則表達式完成,不過最終表達式會變得異常復雜。碰到這些情形時,編寫 Python 代碼進行處理可能反而更好;盡管 Python 代碼比一個精巧的正則表達式要慢些,但它更易理解。
?
Simple Patterns(簡單模式)
We'll start by learning about the simplest possible regular expressions. Since regular expressions are used to operate on strings, we'll begin with the most common task: matching characters.
我們將從最簡單的正則表達式學習開始。由于正則表達式常用于字符串操作,那我們就從最常見的任務:字符匹配 下手。
For a detailed explanation of the computer science underlying regular expressions (deterministic and non-deterministic finite automata), you can refer to almost any textbook on writing compilers.
有關正則表達式底層的計算機科學上的詳細解釋(確定性和非確定性有限自動機),你可以查閱編寫編譯器相關的任何教科書。
?
1. Matching Characters(字符匹配)
Most letters and characters will simply match themselves. For example, the regular expression test will match the string "test" exactly. (You can enable a case-insensitive mode that would let this RE match "Test" or "TEST" as well; more about this later.)
大多數字母和字符一般都會和自身匹配。例如,正則表達式 test 會和字符串“test”完全匹配。(你也可以使用大小寫不敏感模式,它還能讓這個 RE 匹配“Test”或“TEST”;稍后會有更多解釋。)
There are exceptions to this rule; some characters are special, and don't match themselves. Instead, they signal that some out-of-the-ordinary thing should be matched, or they affect other portions of the RE by repeating them. Much of this document is devoted to discussing various metacharacters and what they do.
這個規則當然會有例外;有些字符比較特殊,它們和自身并不匹配,而是會表明應和一些特殊的東西匹配,或者它們會影響到 RE 其它部分的重復次數。本文很大篇幅專門討論了各種元字符及其作用。
Here's a complete list of the metacharacters; their meanings will be discussed in the rest of this HOWTO.
這里有一個元字符的完整列表;其含義會在本指南余下部分進行討論。
The first metacharacters we'll look at are "["?and?"]". They're used for specifying a character class, which is a set of characters that you wish to match. Characters can be listed individually, or a range of characters can be indicated by giving two characters and separating them by a "-". For example, [abc] will match any of the characters "a", "b", or "c"; this is the same as [a-c], which uses a range to express the same set of characters. If you wanted to match only lowercase letters, your RE would be [a-z].
我們首先考察的元字符是 "["?和?"]"。它們常用來指定一個字符類別,所謂字符類別就是你想匹配的一個字符集。字符可以單個列出,也可以用“-”號分隔的兩個給定字符來表示一個字符區間。例如,[abc] 將匹配"a", "b", 或 "c"中的任意一個字符;也可以用區間[a-c]來表示同一字符集,和前者效果一致。如果你只想匹配小寫字母,那么 RE 應寫成 [a-z]。
Metacharacters are not active inside classes. For example, [akm$] will match any of the characters "a", "k", "m", or "$"; "$" is usually a metacharacter, but inside a character class it's stripped of its special nature.
元字符在類別里并不起作用。例如,[akm$]將匹配字符"a", "k", "m", 或 "$" 中的任意一個;"$"通常用作元字符,但在字符類別里,其特性被除去,恢復成普通字符。
You can match the characters not within a range by complementing the set. This is indicated by including a "" as the first character of the class; `""?elsewhere?will?simply?match?the?""` character. For example, [5] will match any character except "5".
你可以用補集來匹配不在區間范圍內的字符。其做法是把"^"作為類別的首個字符;其它地方的"^"只會簡單匹配 "^" 字符本身。例如,[^5] 將匹配除 "5" 之外的任意字符。
Perhaps the most important metacharacter is the backslash, "\". As in Python string literals, the backslash can be followed by various characters to signal various special sequences. It's also used to escape all the metacharacters so you can still match them in patterns; for example, if you need to match a "[" or "\", you can precede them with a backslash to remove their special meaning: \[ or \\.
也許最重要的元字符是反斜杠"\"。 做為 Python 中的字符串字母,反斜杠后面可以加不同的字符以表示不同特殊意義。它也可以用于取消所有的元字符,這樣你就可以在模式中匹配它們了。舉個例子,如果你需要匹配字符 "[" 或 "\",你可以在它們之前用反斜杠來取消它們的特殊意義: \[ 或 \\。
Some of the special sequences beginning with "\" represent predefined sets of characters that are often useful, such as the set of digits, the set of letters, or the set of anything that isn't whitespace. The following predefined special sequences are available:
一些用 "\" 開始的特殊字符所表示的預定義字符集通常是很有用的,象數字集,字母集,或其它非空字符集。下列是可用的預設特殊字符:
\d
-
Matches any decimal digit; this is equivalent to the class [0-9].
匹配任何十進制數;它相當于類 [0-9]。
\D
-
Matches any non-digit character; this is equivalent to the class [^0-9].
匹配任何非數字字符;它相當于類 [^0-9]。
\s
-
Matches any whitespace character; this is equivalent to the class [ \t\n\r\f\v].
匹配任何空白字符;它相當于類 [ \t\n\r\f\v]。
\S
-
Matches any non-whitespace character; this is equivalent to the class [^ \t\n\r\f\v].
匹配任何非空白字符;它相當于類 [^ \t\n\r\f\v]。
\w
-
Matches any alphanumeric character; this is equivalent to the class [a-zA-Z0-9_].
匹配任何字母數字字符;它相當于類 [a-zA-Z0-9_]。
\W
-
Matches any non-alphanumeric character; this is equivalent to the class [^a-zA-Z0-9_].
匹配任何非字母數字字符;它相當于類 [^a-zA-Z0-9_]。
These sequences can be included inside a character class. For example, [\s,.] is a character class that will match any whitespace character, or "," or ".".
這樣特殊字符都可以包含在一個字符類中。如,[\s,.]字符類將匹配任何空白字符或","或"."。
The final metacharacter in this section is .. It matches anything except a newline character, and there's an alternate mode (re.DOTALL) where it will match even a newline. "." is often used where you want to match any character.
本節最后一個元字符是 . 。它匹配除了換行字符外的任何字符,在 alternate 模式(re.DOTALL)下它甚至可以匹配換行。"." 通常被用于你想匹配“任何字符”的地方。
?
2. Repeating Things(重復)
?
Being able to match varying sets of characters is the first thing regular expressions can do that isn't already possible with the methods available on strings. However, if that was the only additional capability of regexes, they wouldn't be much of an advance. Another capability is that you can specify that portions of the RE must be repeated a certain number of times.
正則表達式第一件能做的事是能夠匹配不定長的字符集,而這是其它能作用在字符串上的方法所不能做到的。 不過,如果那是正則表達式唯一的附加功能的話,那么它們也就不那么優秀了。它們的另一個功能就是你可以指定正則表達式的一部分的重復次數。
The first metacharacter for repeating things that we'll look at is *. * doesn't match the literal character "*"; instead, it specifies that the previous character can be matched zero or more times, instead of exactly once.
我們討論的第一個重復功能的元字符是 *。* 并不匹配字母字符 "*";相反,它指定前一個字符可以被匹配零次或更多次,而不是只有一次。
For example, ca*t will match "ct" (0 "a"characters), "cat" (1 "a"), "caaat" (3 "a"characters), and so forth. The RE engine has various internal limitations stemming from the size of C's int type, that will prevent it from matching over 2 billion "a" characters; you probably don't have enough memory to construct a string that large, so you shouldn't run into that limit.
舉個例子,ca*t 將匹配 "ct" (0 個 "a" 字符), "cat" (1 個 "a"), "caaat" (3 個 "a" 字符)等等。RE 引擎有各種來自 C 的整數類型大小的內部限制,以防止它匹配超過2億個 "a" 字符;你也許沒有足夠的內存去建造那么大的字符串,所以將不會累計到那個限制。
Repetitions such as * are greedy; when repeating a RE, the matching engine will try to repeat it as many times as possible. If later portions of the pattern don't match, the matching engine will then back up and try again with few repetitions.
象 * 這樣地重復是“貪婪的”;當重復一個 RE 時,匹配引擎會試著重復盡可能多的次數。如果模式的后面部分沒有被匹配,匹配引擎將退回并再次嘗試更小的重復。
A step-by-step example will make this more obvious. Let's consider the expression a[bcd]*b. This matches the letter "a", zero or more letters from the class [bcd], and finally ends with a "b". Now imagine matching this RE against the string "abcbd".
一步步的示例可以使它更加清晰。讓我們考慮表達式 a[bcd]*b。它匹配字母 "a",零個或更多個來自類 [bcd]中的字母,最后以 "b" 結尾。現在想一想該 RE 對字符串 "abcbd" 的匹配。
| Step | Matched | Explanation |
| 1 | a | The a in the RE matches. |
| 2 | abcbd | The engine matches [bcd]*, going as far as it can, which is to the end of the string. |
| 3 | Failure | The engine tries to match b, but the current position is at the end of the string, so it fails. |
| 4 | abcb | Back up, so that [bcd]* matches one less character. |
| 5 | Failure | Try b again, but the current position is at the last character, which is a "d". |
| 6 | abc | Back up again, so that [bcd]* is only matching "bc". |
| 7 | abcb | Try b again. This time but the character at the current position is "b", so it succeeds. |
The end of the RE has now been reached, and it has matched "abcb". This demonstrates how the matching engine goes as far as it can at first, and if no match is found it will then progressively back up and retry the rest of the RE again and again. It will back up until it has tried zero matches for [bcd]*, and if that subsequently fails, the engine will conclude that the string doesn't match the RE at all.
RE 的結尾部分現在可以到達了,它匹配 "abcb"。這證明了匹配引擎一開始會盡其所能進行匹配,如果沒有匹配然后就逐步退回并反復嘗試 RE 剩下來的部分。直到它退回嘗試匹配 [bcd] 到零次為止,如果隨后還是失敗,那么引擎就會認為該字符串根本無法匹配 RE 。
Another repeating metacharacter is +, which matches one or more times. Pay careful attention to the difference between * and +; * matches zero or more times, so whatever's being repeated may not be present at all, while + requires at least one occurrence. To use a similar example, ca+t will match "cat" (1 "a"), "caaat" (3 "a"'s), but won't match "ct".
另一個重復元字符是 +,表示匹配一或更多次。請注意 * 和 + 之間的不同;* 匹配零或更多次,所以根本就可以不出現,而 + 則要求至少出現一次。用同一個例子,ca+t 就可以匹配 "cat" (1 個 "a"), "caaat" (3 個 "a"), 但不能匹配 "ct"。
There are two more repeating qualifiers. The question mark character, ?, matches either once or zero times; you can think of it as marking something as being optional. For example, home-?brew matches either "homebrew" or "home-brew".
還有更多的限定符。問號 ? 匹配一次或零次;你可以認為它用于標識某事物是可選的。例如:home-?brew 匹配 "homebrew" 或 "home-brew"。
The most complicated repeated qualifier is {m,n}, where m and n are decimal integers. This qualifier means there must be at least m repetitions, and at most n. For example, a/{1,3}b will match "a/b", "a//b", and "a///b". It won't match "ab", which has no slashes, or "ab", which has four.
最復雜的重復限定符是 {m,n},其中 m 和 n 是十進制整數。該限定符的意思是至少有 m 個重復,至多到 n 個重復。舉個例子,a/{1,3}b 將匹配 "a/b","a//b" 和 "a///b"。它不能匹配 "ab" 因為沒有斜杠,也不能匹配 "ab" ,因為有四個。
You can omit either m or n; in that case, a reasonable value is assumed for the missing value. Omitting m is interpreted as a lower limit of 0, while omitting n results in an upper bound of infinity -- actually, the 2 billion limit mentioned earlier, but that might as well be infinity.
你可以忽略 m 或 n;因為會為缺失的值假設一個合理的值。忽略 m 會認為下邊界是 0,而忽略 n 的結果將是上邊界為無窮大 -- 實際上是先前我們提到的 2 兆,但這也許同無窮大一樣。
Readers of a reductionist bent may notice that the three other qualifiers can all be expressed using this notation. {0,} is the same as *, {1,} is equivalent to +, and {0,1} is the same as ?. It's better to use *, +, or ? when you can, simply because they're shorter and easier to read.
細心的讀者也許注意到其他三個限定符都可以用這樣方式來表示。 {0,} 等同于 *,{1,} 等同于 +,而{0,1}則與 ? 相同。如果可以的話,最好使用 *,+,或?。很簡單因為它們更短也再容易懂。
?
Using Regular Expressions(使用正則表達式)
Now that we've looked at some simple regular expressions, how do we actually use them in Python? The re module provides an interface to the regular expression engine, allowing you to compile REs into objects and then perform matches with them.
現在我們已經看了一些簡單的正則表達式,那么我們實際在 Python 中是如何使用它們的呢? re 模塊提供了一個正則表達式引擎的接口,可以讓你將 REs 編譯成對象并用它們來進行匹配。
?
1. Compiling Regular Expressions(編譯正則表達式)
Regular expressions are compiled into RegexObject instances, which have methods for various operations such as searching for pattern matches or performing string substitutions.
正則表達式被編譯成 RegexObject 實例,可以為不同的操作提供方法,如模式匹配搜索或字符串替換。
re.compile() also accepts an optional flags argument, used to enable various special features and syntax variations. We'll go over the available settings later, but for now a single example will do:
re.compile() 也接受可選的標志參數,常用來實現不同的特殊功能和語法變更。我們稍后將查看所有可用的設置,但現在只舉一個例子:
The RE is passed to re.compile() as a string. REs are handled as strings because regular expressions aren't part of the core Python language, and no special syntax was created for expressing them. (There are applications that don't need REs at all, so there's no need to bloat the language specification by including them.) Instead, the re module is simply a C extension module included with Python, just like the socket or zlib module.
RE 被做為一個字符串發送給 re.compile()。REs 被處理成字符串是因為正則表達式不是 Python 語言的核心部分,也沒有為它創建特定的語法。(應用程序根本就不需要 REs,因此沒必要包含它們去使語言說明變得臃腫不堪。)而 re 模塊則只是以一個 C 擴展模塊的形式來被 Python 包含,就象 socket 或 zlib 模塊一樣。
Putting REs in strings keeps the Python language simpler, but has one disadvantage which is the topic of the next section.
將 REs 作為字符串以保證 Python 語言的簡潔,但這樣帶來的一個麻煩就是象下節標題所講的。
?
2. The Backslash Plague(反斜杠的麻煩)
As stated earlier, regular expressions use the backslash character ("\") to indicate special forms or to allow special characters to be used without invoking their special meaning. This conflicts with Python's usage of the same character for the same purpose in string literals.
在早期規定中,正則表達式用反斜杠字符 ("\") 來表示特殊格式或允許使用特殊字符而不調用它的特殊用法。這就與 Python 在字符串中的那些起相同作用的相同字符產生了沖突。
Let's say you want to write a RE that matches the string "\section", which might be found in a LATEX file. To figure out what to write in the program code, start with the desired string to be matched. Next, you must escape any backslashes and other metacharacters by preceding them with a backslash, resulting in the string "\\section". The resulting string that must be passed to re.compile() must be \\section. However, to express this as a Python string literal, both backslashes must be escaped again.
讓我們舉例說明,你想寫一個 RE 以匹配字符串 "\section",可能是在一個 LATEX 文件查找。為了要在程序代碼中判斷,首先要寫出想要匹配的字符串。接下來你需要在所有反斜杠和元字符前加反斜杠來取消其特殊意義。
| Characters(字符) | Stage(階段) |
| \section | Text string to be matched(要匹配的字符串) |
| \\section | Escaped backslash for re.compile(為 re.compile 取消反斜杠的特殊意義) |
| "\\\\section" | Escaped backslashes for a string literal(為字符串取消反斜杠) |
In short, to match a literal backslash, one has to write '\\\\' as the RE string, because the regular expression must be "\\", and each backslash must be expressed as "\\" inside a regular Python string literal. In REs that feature backslashes repeatedly, this leads to lots of repeated backslashes and makes the resulting strings difficult to understand.
簡單地說,為了匹配一個反斜杠,不得不在 RE 字符串中寫 '\\\\',因為正則表達式中必須是 "\\",而每個反斜杠按 Python 字符串字母表示的常規必須表示成 "\\"。在 REs 中反斜杠的這個重復特性會導致大量重復的反斜杠,而且所生成的字符串也很難懂。
The solution is to use Python's raw string notation for regular expressions; backslashes are not handled in any special way in a string literal prefixed with "r", so r"\n" is a two-character string containing "\" and "n", while "\n" is a one-character string containing a newline. Frequently regular expressions will be expressed in Python code using this raw string notation.
解決的辦法就是為正則表達式使用 Python 的 raw 字符串表示;在字符串前加個 "r" 反斜杠就不會被任何特殊方式處理,所以 r"\n" 就是包含"\" 和 "n" 的兩個字符,而 "\n" 則是一個字符,表示一個換行。正則表達式通常在 Python 代碼中都是用這種 raw 字符串表示。
| Regular String(常規字符串) | Raw string(Raw 字符串) |
| "ab*" | r"ab*" |
| "\\\\section" | r"\\section" |
| "\\w+\\s+\\1" | r"\w+\s+\1" |
?
3. Performing Matches(執行匹配)
Once you have an object representing a compiled regular expression, what do you do with it? RegexObject instances have several methods and attributes. Only the most significant ones will be covered here; consult the Library Reference for a complete listing.
一旦你有了已經編譯了的正則表達式的對象,你要用它做什么呢?RegexObject 實例有一些方法和屬性。這里只顯示了最重要的幾個,如果要看完整的列表請查閱 Library Refference。
| Method/Attribute(方法/屬性) | Purpose(作用) |
| match() | Determine if the RE matches at the beginning of the string. |
| search() | Scan through a string, looking for any location where this RE matches. |
| findall() | Find all substrings where the RE matches, and returns them as a list. |
| finditer() | Find all substrings where the RE matches, and returns them as an iterator. |
match() and search() return None if no match can be found. If they're successful, a MatchObject instance is returned, containing information about the match: where it starts and ends, the substring it matched, and more.
如果沒有匹配到的話,match() 和 search() 將返回 None。如果成功的話,就會返回一個 MatchObject 實例,其中有這次匹配的信息:它是從哪里開始和結束,它所匹配的子串等等。
You can learn about this by interactively experimenting with the re module. If you have Tkinter available, you may also want to look at Tools/scripts/redemo.py, a demonstration program included with the Python distribution. It allows you to enter REs and strings, and displays whether the RE matches or fails. redemo.py can be quite useful when trying to debug a complicated RE. Phil Schwartz's Kodos is also an interactive tool for developing and testing RE patterns. This HOWTO will use the standard Python interpreter for its examples.
你可以用采用人機對話并用 re 模塊實驗的方式來學習它。如果你有 Tkinter 的話,你也許可以考慮參考一下 Tools/scripts/redemo.py,一個包含在 Python 發行版里的示范程序。
First, run the Python interpreter, import the re module, and compile a RE:
首先,運行 Python 解釋器,導入 re 模塊并編譯一個 RE:
Now, you can try matching various strings against the RE [a-z]+. An empty string shouldn't match at all, since + means 'one or more repetitions'. match() should return None in this case, which will cause the interpreter to print no output. You can explicitly print the result of match() to make this clear.
現在,你可以試著用 RE 的 [a-z]+ 去匹配不同的字符串。一個空字符串將根本不能匹配,因為 + 的意思是 “一個或更多的重復次數”。 在這種情況下 match() 將返回 None,因為它使解釋器沒有輸出。你可以明確地打印出 match() 的結果來弄清這一點。
Now, let's try it on a string that it should match, such as "tempo". In this case, match() will return a MatchObject, so you should store the result in a variable for later use.
現在,讓我們試著用它來匹配一個字符串,如 "tempo"。這時,match() 將返回一個 MatchObject。因此你可以將結果保存在變量里以便后面使用。
Now you can query the MatchObject for information about the matching string. MatchObject instances also have several methods and attributes; the most important ones are:
現在你可以查詢 MatchObject 關于匹配字符串的相關信息了。MatchObject 實例也有幾個方法和屬性;最重要的那些如下所示:
| Method/Attribute(方法/屬性) | Purpose(作用) |
| group() | Return the string matched by the RE |
| start() | Return the starting position of the match |
| end() | Return the ending position of the match |
| span() | Return a tuple containing the (start, end) positions of the match |
Trying these methods will soon clarify their meaning:
試試這些方法不久就會清楚它們的作用了:
group() returns the substring that was matched by the RE. start() and end() return the starting and ending index of the match. span() returns both start and end indexes in a single tuple. Since the match method only checks if the RE matches at the start of a string, start() will always be zero. However, the search method of RegexObject instances scans through the string, so the match may not start at zero in that case.
group() 返回 RE 匹配的子串。start() 和 end() 返回匹配開始和結束時的索引。span() 則用單個元組把開始和結束時的索引一起返回。因為匹配方法檢查到如果 RE 在字符串開始處開始匹配,那么 start() 將總是為零。然而, RegexObject 實例的 search 方法掃描下面的字符串的話,在這種情況下,匹配開始的位置就也許不是零了。
In actual programs, the most common style is to store the MatchObject in a variable, and then check if it was None. This usually looks like:
在實際程序中,最常見的作法是將 MatchObject 保存在一個變量里,然后檢查它是否為 None,通常如下所示:
Two RegexObject methods return all of the matches for a pattern. findall() returns a list of matching strings:
兩個 RegexObject 方法返回所有匹配模式的子串。findall()返回一個匹配字符串列表:
findall() has to create the entire list before it can be returned as the result. In Python 2.2, the finditer() method is also available, returning a sequence of MatchObject instances as an iterator.
findall() 在它返回結果時不得不創建一個列表。在 Python 2.2中,也可以用 finditer() 方法。
?
4. Module-Level Functions(模塊級函數)
You don't have to produce a RegexObject and call its methods; the re module also provides top-level functions called match(), search(), sub(), and so forth. These functions take the same arguments as the corresponding RegexObject method, with the RE string added as the first argument, and still return either None or a MatchObject instance.
你不一定要產生一個 RegexObject 對象然后再調用它的方法;re 模塊也提供了頂級函數調用如 match()、search()、sub() 等等。這些函數使用 RE 字符串作為第一個參數,而后面的參數則與相應 RegexObject 的方法參數相同,返回則要么是 None 要么就是一個 MatchObject 的實例。
Under the hood, these functions simply produce a RegexObject for you and call the appropriate method on it. They also store the compiled object in a cache, so future calls using the same RE are faster.
Under the hood, 這些函數簡單地產生一個 RegexOject 并在其上調用相應的方法。它們也在緩存里保存編譯后的對象,因此在將來調用用到相同 RE 時就會更快。
Should you use these module-level functions, or should you get the RegexObject and call its methods yourself? That choice depends on how frequently the RE will be used, and on your personal coding style. If a RE is being used at only one point in the code, then the module functions are probably more convenient. If a program contains a lot of regular expressions, or re-uses the same ones in several locations, then it might be worthwhile to collect all the definitions in one place, in a section of code that compiles all the REs ahead of time. To take an example from the standard library, here's an extract from xmllib.py: 你將使用這些模塊級函數,還是先得到一個 RegexObject 再調用它的方法呢?如何選擇依賴于怎樣用 RE 更有效率以及你個人編碼風格。如果一個 RE 在代碼中只做用一次的話,那么模塊級函數也許更方便。如果程序包含很多的正則表達式,或在多處復用同一個的話,那么將全部定義放在一起,在一段代碼中提前編譯所有的 REs 更有用。從標準庫中看一個例子,這是從 xmllib.py 文件中提取出來的:
切換行號顯示 切換行號顯示 切換行號顯示 1 ref = re.compile( ... ) 2 entityref = re.compile( ... ) 3 charref = re.compile( ... ) 4 starttagopen = re.compile( ... )I generally prefer to work with the compiled object, even for one-time uses, but few people will be as much of a purist about this as I am.
我通常更喜歡使用編譯對象,甚至它只用一次,but few people will be as much of a purist about this as I am。
?
5. Compilation Flags(編譯標志)
Compilation flags let you modify some aspects of how regular expressions work. Flags are available in the re module under two names, a long name such as IGNORECASE, and a short, one-letter form such as I. (If you're familiar with Perl's pattern modifiers, the one-letter forms use the same letters; the short form of re.VERBOSE is re.X, for example.) Multiple flags can be specified by bitwise OR-ing them; re.I | re.M sets both the I and M flags, for example.
編譯標志讓你可以修改正則表達式的一些運行方式。在 re 模塊中標志可以使用兩個名字,一個是全名如 IGNORECASE,一個是縮寫,一字母形式如 I。(如果你熟悉 Perl 的模式修改,一字母形式使用同樣的字母;例如 re.VERBOSE的縮寫形式是 re.X。)多個標志可以通過按位 OR-ing 它們來指定。如 re.I | re.M 被設置成 I 和 M 標志:
Here's a table of the available flags, followed by a more detailed explanation of each one.
這有個可用標志表,對每個標志后面都有詳細的說明。
| Flag(標志) | Meaning(含義) |
| DOTALL, S | Make . match any character, including newlines |
| IGNORECASE, I | Do case-insensitive matches |
| LOCALE, L | Do a locale-aware match |
| MULTILINE, M | Multi-line matching, affecting and $[[BR]]多行匹配,影響 和 $ |
| VERBOSE, X | Enable verbose REs, which can be organized more cleanly and understandably. |
I
IGNORECASE
-
Perform case-insensitive matching; character class and literal strings will match letters by ignoring case. For example, [A-Z] will match lowercase letters, too, and Spam will match "Spam", "spam", or "spAM". This lowercasing doesn't take the current locale into account; it will if you also set the LOCALE flag.
使匹配對大小寫不敏感;字符類和字符串匹配字母時忽略大小寫。舉個例子,[A-Z]也可以匹配小寫字母,Spam 可以匹配 "Spam", "spam", 或 "spAM"。這個小寫字母并不考慮當前位置。
L
LOCALE
-
Make \w, \W, \b, and \B, dependent on the current locale.
影響 \w, \W, \b, 和 \B,這取決于當前的本地化設置。Locales are a feature of the C library intended to help in writing programs that take account of language differences. For example, if you're processing French text, you'd want to be able to write \w+ to match words, but \w only matches the character class [A-Za-z]; it won't match "é" or "?". If your system is configured properly and a French locale is selected, certain C functions will tell the program that "é" should also be considered a letter. Setting the LOCALE flag when compiling a regular expression will cause the resulting compiled object to use these C functions for \w; this is slower, but also enables \w+ to match French words as you'd expect.
locales 是 C 語言庫中的一項功能,是用來為需要考慮不同語言的編程提供幫助的。舉個例子,如果你正在處理法文文本,你想用 \w+ 來匹配文字,但 \w 只匹配字符類 [A-Za-z];它并不能匹配 "é" 或 "?"。如果你的系統配置適當且本地化設置為法語,那么內部的 C 函數將告訴程序 "é" 也應該被認為是一個字母。當在編譯正則表達式時使用 LOCALE 標志會得到用這些 C 函數來處理 \w 后的編譯對象;這會更慢,但也會象你希望的那樣可以用 \w+ 來匹配法文文本。
M
MULTILINE
-
(^ and $ haven't been explained yet; they'll be introduced in section 4.1.)
(此時 ^ 和 $ 不會被解釋; 它們將在 4.1 節被介紹.)Usually matches only at the beginning of the string, and $ matches only at the end of the string and immediately before the newline (if any) at the end of the string. When this flag is specified, matches at the beginning of the string and at the beginning of each line within the string, immediately following each newline. Similarly, the $ metacharacter matches either at the end of the string and at the end of each line (immediately preceding each newline).
使用 只匹配字符串的開始,而 $ 則只匹配字符串的結尾和直接在換行前(如果有的話)的字符串結尾。當本標志指定后, 匹配字符串的開始和字符串中每行的開始。同樣的, $ 元字符匹配字符串結尾和字符串中每行的結尾(直接在每個換行之前)。
S
DOTALL
-
Makes the "." special character match any character at all, including a newline; without this flag, "." will match anything except a newline.
使 "." 特殊字符完全匹配任何字符,包括換行;沒有這個標志, "." 匹配除了換行外的任何字符。
X
VERBOSE
-
This flag allows you to write regular expressions that are more readable by granting you more flexibility in how you can format them. When this flag has been specified, whitespace within the RE string is ignored, except when the whitespace is in a character class or preceded by an unescaped backslash; this lets you organize and indent the RE more clearly. It also enables you to put comments within a RE that will be ignored by the engine; comments are marked by a "#" that's neither in a character class or preceded by an unescaped backslash.
該標志通過給予你更靈活的格式以便你將正則表達式寫得更易于理解。當該標志被指定時,在 RE 字符串中的空白符被忽略,除非該空白符在字符類中或在反斜杠之后;這可以讓你更清晰地組織和縮進 RE。它也可以允許你將注釋寫入 RE,這些注釋會被引擎忽略;注釋用 "#"號 來標識,不過該符號不能在字符串或反斜杠之后。For example, here's a RE that uses re.VERBOSE; see how much easier it is to read?
切換行號顯示 切換行號顯示 切換行號顯示 1 charref = re.compile(r""" 2 &[#] # Start of a numeric entity reference 3 ( 4 [0-9]+[^0-9] # Decimal form 5 | 0[0-7]+[^0-7] # Octal form 6 | x[0-9a-fA-F]+[^0-9a-fA-F] # Hexadecimal form 7 ) 8 """, re.VERBOSE)
舉個例子,這里有一個使用 re.VERBOSE 的 RE;看看讀它輕松了多少?Without the verbose setting, the RE would look like this:
切換行號顯示 切換行號顯示 切換行號顯示 1 charref = re.compile("&#([0-9]+[^0-9]" 2 "|0[0-7]+[^0-7]" 3 "|x[0-9a-fA-F]+[^0-9a-fA-F])")
沒有 verbose 設置, RE 會看起來象這樣:In the above example, Python's automatic concatenation of string literals has been used to break up the RE into smaller pieces, but it's still more difficult to understand than the version using re.VERBOSE.
在上面的例子里,Python 的字符串自動連接可以用來將 RE 分成更小的部分,但它比用 re.VERBOSE 標志時更難懂。
?
More Pattern Power(更多模式功能)
So far we've only covered a part of the features of regular expressions. In this section, we'll cover some new metacharacters, and how to use groups to retrieve portions of the text that was matched.
到目前為止,我們只展示了正則表達式的一部分功能。在本節,我們將展示一些新的元字符和如何使用組來檢索被匹配的文本部分。
?
1. More Metacharacters(更多的元字符)
There are some metacharacters that we haven't covered yet. Most of them will be covered in this section.
還有一些我們還沒展示的元字符,其中的大部分將在本節展示。
Some of the remaining metacharacters to be discussed are zero-width assertions. They don't cause the engine to advance through the string; instead, they consume no characters at all, and simply succeed or fail. For example, \b is an assertion that the current position is located at a word boundary; the position isn't changed by the \b at all. This means that zero-width assertions should never be repeated, because if they match once at a given location, they can obviously be matched an infinite number of times.
剩下來要討論的一部分元字符是零寬界定符(zero-width assertions)。它們并不會使引擎在處理字符串時更快;相反,它們根本就沒有對應任何字符,只是簡單的成功或失敗。舉個例子, \b 是一個在單詞邊界定位當前位置的界定符(assertions),這個位置根本就不會被 \b 改變。這意味著零寬界定符(zero-width assertions)將永遠不會被重復,因為如果它們在給定位置匹配一次,那么它們很明顯可以被匹配無數次。
|
-
Alternation, or the "or" operator. If A and B are regular expressions, A|B will match any string that matches either "A" or "B". | has very low precedence in order to make it work reasonably when you're alternating multi-character strings. Crow|Servo will match either "Crow" or "Servo", not "Cro", a "w" or an "S", and "ervo".
可選項,或者 "or" 操作符。如果 A 和 B 是正則表達式,A|B 將匹配任何匹配了 "A" 或 "B" 的字符串。| 的優先級非常低,是為了當你有多字符串要選擇時能適當地運行。Crow|Servo 將匹配"Crow" 或 "Servo", 而不是 "Cro", 一個 "w" 或 一個 "S", 和 "ervo"。To match a literal "|", use \|, or enclose it inside a character class, as in [|].
為了匹配字母 "|",可以用 \|,或將其包含在字符類中,如[|]。
^
-
Matches at the beginning of lines. Unless the MULTILINE flag has been set, this will only match at the beginning of the string. In MULTILINE mode, this also matches immediately after each newline within the string.
匹配行首。除非設置 MULTILINE 標志,它只是匹配字符串的開始。在 MULTILINE 模式里,它也可以直接匹配字符串中的每個換行。For example, if you wish to match the word "From" only at the beginning of a line, the RE to use is ^From.
切換行號顯示 切換行號顯示 切換行號顯示 1 >>> print re.search('^From', 'From Here to Eternity') 2 <re.MatchObject instance at 80c1520> 3 >>> print re.search('^From', 'Reciting From Memory') 4 None
例如,如果你只希望匹配在行首單詞 "From",那么 RE 將用 ^From。
$
-
Matches at the end of a line, which is defined as either the end of the string, or any location followed by a newline character.
切換行號顯示 切換行號顯示 切換行號顯示 1 >>> print re.search('}$', '{block}') 2 <re.MatchObject instance at 80adfa8> 3 >>> print re.search('}$', '{block} ') 4 None 5 >>> print re.search('}$', '{block}\n') 6 <re.MatchObject instance at 80adfa8>
匹配行尾,行尾被定義為要么是字符串尾,要么是一個換行字符后面的任何位置。To match a literal "$", use \$ or enclose it inside a character class, as in [$].
匹配一個 "$",使用 \$ 或將其包含在字符類中,如[$]。
\A
-
Matches only at the start of the string. When not in MULTILINE mode, \A and are effectively the same. In MULTILINE mode, however, they're different; \A still matches only at the beginning of the string, but may match at any location inside the string that follows a newline character.
只匹配字符串首。當不在 MULTILINE 模式,\A 和 實際上是一樣的。然而,在 MULTILINE 模式里它們是不同的;\A 只是匹配字符串首,而 還可以匹配在換行符之后字符串的任何位置。
\Z
-
Matches only at the end of the string.
只匹配字符串尾。
\b
-
Word boundary. This is a zero-width assertion that matches only at the beginning or end of a word. A word is defined as a sequence of alphanumeric characters, so the end of a word is indicated by whitespace or a non-alphanumeric character.
單詞邊界。這是個零寬界定符(zero-width assertions)只用以匹配單詞的詞首和詞尾。單詞被定義為一個字母數字序列,因此詞尾就是用空白符或非字母數字符來標示的。The following example matches "class" only when it's a complete word; it won't match when it's contained inside another word.
切換行號顯示 切換行號顯示 切換行號顯示 1 >>> p = re.compile(r'\bclass\b') 2 >>> print p.search('no class at all') 3 <re.MatchObject instance at 80c8f28> 4 >>> print p.search('the declassified algorithm') 5 None 6 >>> print p.search('one subclass is') 7 None
下面的例子只匹配 "class" 整個單詞;而當它被包含在其他單詞中時不匹配。There are two subtleties you should remember when using this special sequence. First, this is the worst collision between Python's string literals and regular expression sequences. In Python's string literals, "\b" is the backspace character, ASCII value 8. If you're not using raw strings, then Python will convert the "\b" to a backspace, and your RE won't match as you expect it to. The following example looks the same as our previous RE, but omits the "r" in front of the RE string.
切換行號顯示 切換行號顯示 切換行號顯示 1 >>> p = re.compile('\bclass\b') 2 >>> print p.search('no class at all') 3 None 4 >>> print p.search('\b' + 'class' + '\b') 5 <re.MatchObject instance at 80c3ee0>
當用這個特殊序列時你應該記住這里有兩個微妙之處。第一個是 Python 字符串和正則表達式之間最糟的沖突。在 Python 字符串里,"\b" 是反斜杠字符,ASCII值是8。如果你沒有使用 raw 字符串時,那么 Python 將會把 "\b" 轉換成一個回退符,你的 RE 將無法象你希望的那樣匹配它了。下面的例子看起來和我們前面的 RE 一樣,但在 RE 字符串前少了一個 "r" 。Second, inside a character class, where there's no use for this assertion, \b represents the backspace character, for compatibility with Python's string literals.
第二個在字符類中,這個限定符(assertion)不起作用,\b 表示回退符,以便與 Python 字符串兼容。
\B
-
Another zero-width assertion, this is the opposite of \b, only matching when the current position is not at a word boundary.
另一個零寬界定符(zero-width assertions),它正好同 \b 相反,只在當前位置不在單詞邊界時匹配。
?
2. Grouping(分組)
Frequently you need to obtain more information than just whether the RE matched or not. Regular expressions are often used to dissect strings by writing a RE divided into several subgroups which match different components of interest. For example, an RFC-822 header line is divided into a header name and a value, separated by a ":". This can be handled by writing a regular expression which matches an entire header line, and has one group which matches the header name, and another group which matches the header's value.
你經常需要得到比 RE 是否匹配還要多的信息。正則表達式常常用來分析字符串,編寫一個 RE 匹配感興趣的部分并將其分成幾個小組。舉個例子,一個 RFC-822 的頭部用 ":" 隔成一個頭部名和一個值,這就可以通過編寫一個正則表達式匹配整個頭部,用一組匹配頭部名,另一組匹配頭部值的方式來處理。
Groups are marked by the "(", ")" metacharacters. "(" and ")" have much the same meaning as they do in mathematical expressions; they group together the expressions contained inside them. For example, you can repeat the contents of a group with a repeating qualifier, such as *, +, ?, or {m,n}. For example, (ab)* will match zero or more repetitions of "ab".
組是通過 "(" 和 ")" 元字符來標識的。 "(" 和 ")" 有很多在數學表達式中相同的意思;它們一起把在它們里面的表達式組成一組。舉個例子,你可以用重復限制符,象 *, +, ?, 和 {m,n},來重復組里的內容,比如說(ab)* 將匹配零或更多個重復的 "ab"。
Groups indicated with "(", ")" also capture the starting and ending index of the text that they match; this can be retrieved by passing an argument to group(), start(), end(), and span(). Groups are numbered starting with 0. Group 0 is always present; it's the whole RE, so MatchObject methods all have group 0 as their default argument. Later we'll see how to express groups that don't capture the span of text that they match.
組用 "(" 和 ")" 來指定,并且得到它們匹配文本的開始和結尾索引;這就可以通過一個參數用 group()、start()、end() 和 span() 來進行檢索。組是從 0 開始計數的。組 0 總是存在;它就是整個 RE,所以 MatchObject 的方法都把組 0 作為它們缺省的參數。稍后我們將看到怎樣表達不能得到它們所匹配文本的 span。
Subgroups are numbered from left to right, from 1 upward. Groups can be nested; to determine the number, just count the opening parenthesis characters, going from left to right.
小組是從左向右計數的,從1開始。組可以被嵌套。計數的數值可以能過從左到右計算打開的括號數來確定。
group() can be passed multiple group numbers at a time, in which case it will return a tuple containing the corresponding values for those groups.
group() 可以一次輸入多個組號,在這種情況下它將返回一個包含那些組所對應值的元組。
The groups() method returns a tuple containing the strings for all the subgroups, from 1 up to however many there are.
?
>>> m.groups() ('abc', 'b')Backreferences in a pattern allow you to specify that the contents of an earlier capturing group must also be found at the current location in the string. For example, \1 will succeed if the exact contents of group 1 can be found at the current position, and fails otherwise. Remember that Python's string literals also use a backslash followed by numbers to allow including arbitrary characters in a string, so be sure to use a raw string when incorporating backreferences in a RE.
For example, the following RE detects doubled words in a string.
?
>>> p = re.compile(r'(\b\w+)\s+\1') >>> p.search('Paris in the the spring').group() 'the the'Backreferences like this aren't often useful for just searching through a string -- there are few text formats which repeat data in this way -- but you'll soon find out that they're very useful when performing string substitutions.
?
4.3 Non-capturing and Named Groups
Elaborate REs may use many groups, both to capture substrings of interest, and to group and structure the RE itself. In complex REs, it becomes difficult to keep track of the group numbers. There are two features which help with this problem. Both of them use a common syntax for regular expression extensions, so we'll look at that first.
Perl 5 added several additional features to standard regular expressions, and the Python re module supports most of them. It would have been difficult to choose new single-keystroke metacharacters or new special sequences beginning with "\" to represent the new features without making Perl's regular expressions confusingly different from standard REs. If you chose "&" as a new metacharacter, for example, old expressions would be assuming that "&" was a regular character and wouldn't have escaped it by writing \& or [&].
The solution chosen by the Perl developers was to use (?...) as the extension syntax. "?" immediately after a parenthesis was a syntax error because the "?" would have nothing to repeat, so this didn't introduce any compatibility problems. The characters immediately after the "?" indicate what extension is being used, so (?=foo) is one thing (a positive lookahead assertion) and (?:foo) is something else (a non-capturing group containing the subexpression foo).
Python adds an extension syntax to Perl's extension syntax. If the first character after the question mark is a "P", you know that it's an extension that's specific to Python. Currently there are two such extensions: (?P<name>...) defines a named group, and (?P=name) is a backreference to a named group. If future versions of Perl 5 add similar features using a different syntax, the re module will be changed to support the new syntax, while preserving the Python-specific syntax for compatibility's sake.
Now that we've looked at the general extension syntax, we can return to the features that simplify working with groups in complex REs. Since groups are numbered from left to right and a complex expression may use many groups, it can become difficult to keep track of the correct numbering, and modifying such a complex RE is annoying. Insert a new group near the beginning, and you change the numbers of everything that follows it.
First, sometimes you'll want to use a group to collect a part of a regular expression, but aren't interested in retrieving the group's contents. You can make this fact explicit by using a non-capturing group: (?:...), where you can put any other regular expression inside the parentheses.
?
>>> m = re.match("([abc])+", "abc") >>> m.groups() ('c',) >>> m = re.match("(?:[abc])+", "abc") >>> m.groups() ()Except for the fact that you can't retrieve the contents of what the group matched, a non-capturing group behaves exactly the same as a capturing group; you can put anything inside it, repeat it with a repetition metacharacter such as "*", and nest it within other groups (capturing or non-capturing). (?:...) is particularly useful when modifying an existing group, since you can add new groups without changing how all the other groups are numbered. It should be mentioned that there's no performance difference in searching between capturing and non-capturing groups; neither form is any faster than the other.
The second, and more significant, feature is named groups; instead of referring to them by numbers, groups can be referenced by a name.
The syntax for a named group is one of the Python-specific extensions: (?P<name>...). name is, obviously, the name of the group. Except for associating a name with a group, named groups also behave identically to capturing groups. The MatchObject methods that deal with capturing groups all accept either integers, to refer to groups by number, or a string containing the group name. Named groups are still given numbers, so you can retrieve information about a group in two ways:
?
>>> p = re.compile(r'(?P<word>\b\w+\b)') >>> m = p.search( '(((( Lots of punctuation )))' ) >>> m.group('word') 'Lots' >>> m.group(1) 'Lots'Named groups are handy because they let you use easily-remembered names, instead of having to remember numbers. Here's an example RE from the imaplib module:
?
InternalDate = re.compile(r'INTERNALDATE "' r'(?P<day>[ 123][0-9])-(?P<mon>[A-Z][a-z][a-z])-' r'(?P<year>[0-9][0-9][0-9][0-9])' r' (?P<hour>[0-9][0-9]):(?P<min>[0-9][0-9]):(?P<sec>[0-9][0-9])' r' (?P<zonen>[-+])(?P<zoneh>[0-9][0-9])(?P<zonem>[0-9][0-9])' r'"')It's obviously much easier to retrieve m.group('zonem'), instead of having to remember to retrieve group 9.
Since the syntax for backreferences, in an expression like (...)\1, refers to the number of the group there's naturally a variant that uses the group name instead of the number. This is also a Python extension: (?P=name) indicates that the contents of the group called name should again be found at the current point. The regular expression for finding doubled words, (\b\w+)\s+\1 can also be written as (?P<word>\b\w+)\s+(?P=word):
?
>>> p = re.compile(r'(?P<word>\b\w+)\s+(?P=word)') >>> p.search('Paris in the the spring').group() 'the the'?
4.4 Lookahead Assertions
Another zero-width assertion is the lookahead assertion. Lookahead assertions are available in both positive and negative form, and look like this:
?
(?=...)?
An example will help make this concrete by demonstrating a case where a lookahead is useful. Consider a simple pattern to match a filename and split it apart into a base name and an extension, separated by a ".". For example, in "news.rc", "news"is the base name, and "rc" is the filename's extension.
The pattern to match this is quite simple:
.*[.].*$
Notice that the "." needs to be treated specially because it's a metacharacter; I've put it inside a character class. Also notice the trailing $; this is added to ensure that all the rest of the string must be included in the extension. This regular expression matches "foo.bar" and "autoexec.bat" and "sendmail.cf" and "printers.conf".
Now, consider complicating the problem a bit; what if you want to match filenames where the extension is not "bat"? Some incorrect attempts:
.*[.][^b].*$
The first attempt above tries to exclude "bat" by requiring that the first character of the extension is not a "b". This is wrong, because the pattern also doesn't match "foo.bar".
.*[.]([^b]..|.[^a].|..[^t])$
The expression gets messier when you try to patch up the first solution by requiring one of the following cases to match: the first character of the extension isn't "b"; the second character isn't "a"; or the third character isn't "t". This accepts "foo.bar" and rejects "autoexec.bat", but it requires a three-letter extension and won't accept a filename with a two-letter extension such as "sendmail.cf". We'll complicate the pattern again in an effort to fix it.
.*[.]([^b].?.?|.[^a]?.?|..?[^t]?)$
In the third attempt, the second and third letters are all made optional in order to allow matching extensions shorter than three characters, such as "sendmail.cf".
The pattern's getting really complicated now, which makes it hard to read and understand. Worse, if the problem changes and you want to exclude both "bat" and "exe" as extensions, the pattern would get even more complicated and confusing.
A negative lookahead cuts through all this:
.*[.](?!bat$).*$
The lookahead means: if the expression bat doesn't match at this point, try the rest of the pattern; if bat$ does match, the whole pattern will fail. The trailing $ is required to ensure that something like "sample.batch", where the extension only starts with "bat", will be allowed.
Excluding another filename extension is now easy; simply add it as an alternative inside the assertion. The following pattern excludes filenames that end in either "bat" or "exe":
.*[.](?!bat$|exe$).*$
?
5 Modifying Strings
Up to this point, we've simply performed searches against a static string. Regular expressions are also commonly used to modify a string in various ways, using the following RegexObject methods:
| split() | Split the string into a list, splitting it wherever the RE matches |
| sub() | Find all substrings where the RE matches, and replace them with a different string |
| subn() | Does the same thing as sub(), but returns the new string and the number of replacements |
?
5.1 Splitting Strings
The split() method of a RegexObject splits a string apart wherever the RE matches, returning a list of the pieces. It's similar to the split() method of strings but provides much more generality in the delimiters that you can split by; split() only supports splitting by whitespace or by a fixed string. As you'd expect, there's a module-level re.split() function, too.
?
| split( | string [, maxsplit = 0]) |
You can limit the number of splits made, by passing a value for maxsplit. When maxsplit is nonzero, at most maxsplit splits will be made, and the remainder of the string is returned as the final element of the list. In the following example, the delimiter is any sequence of non-alphanumeric characters.
?
>>> p = re.compile(r'\W+') >>> p.split('This is a test, short and sweet, of split().') ['This', 'is', 'a', 'test', 'short', 'and', 'sweet', 'of', 'split', ''] >>> p.split('This is a test, short and sweet, of split().', 3) ['This', 'is', 'a', 'test, short and sweet, of split().']Sometimes you're not only interested in what the text between delimiters is, but also need to know what the delimiter was. If capturing parentheses are used in the RE, then their values are also returned as part of the list. Compare the following calls:
?
>>> p = re.compile(r'\W+') >>> p2 = re.compile(r'(\W+)') >>> p.split('This... is a test.') ['This', 'is', 'a', 'test', ''] >>> p2.split('This... is a test.') ['This', '... ', 'is', ' ', 'a', ' ', 'test', '.', '']The module-level function re.split() adds the RE to be used as the first argument, but is otherwise the same.
?
>>> re.split('[\W]+', 'Words, words, words.') ['Words', 'words', 'words', ''] >>> re.split('([\W]+)', 'Words, words, words.') ['Words', ', ', 'words', ', ', 'words', '.', ''] >>> re.split('[\W]+', 'Words, words, words.', 1) ['Words', 'words, words.']?
5.2 Search and Replace
Another common task is to find all the matches for a pattern, and replace them with a different string. The sub() method takes a replacement value, which can be either a string or a function, and the string to be processed.
?
| sub( | replacement, string[, count = 0]) |
The optional argument count is the maximum number of pattern occurrences to be replaced; count must be a non-negative integer. The default value of 0 means to replace all occurrences.
Here's a simple example of using the sub() method. It replaces colour names with the word "colour":
?
>>> p = re.compile( '(blue|white|red)') >>> p.sub( 'colour', 'blue socks and red shoes') 'colour socks and colour shoes' >>> p.sub( 'colour', 'blue socks and red shoes', count=1) 'colour socks and red shoes'The subn() method does the same work, but returns a 2-tuple containing the new string value and the number of replacements that were performed:
?
>>> p = re.compile( '(blue|white|red)') >>> p.subn( 'colour', 'blue socks and red shoes') ('colour socks and colour shoes', 2) >>> p.subn( 'colour', 'no colours at all') ('no colours at all', 0)Empty matches are replaced only when they're not adjacent to a previous match.
?
>>> p = re.compile('x*') >>> p.sub('-', 'abxd') '-a-b-d-'If replacement is a string, any backslash escapes in it are processed. That is, "\n" is converted to a single newline character, "\r" is converted to a carriage return, and so forth. Unknown escapes such as "\j" are left alone. Backreferences, such as "\6", are replaced with the substring matched by the corresponding group in the RE. This lets you incorporate portions of the original text in the resulting replacement string.
This example matches the word "section" followed by a string enclosed in "{", "}", and changes "section" to "subsection":
?
>>> p = re.compile('section{ ( [^}]* ) }', re.VERBOSE) >>> p.sub(r'subsection{\1}','section{First} section{second}') 'subsection{First} subsection{second}'There's also a syntax for referring to named groups as defined by the (?P<name>...) syntax. "\g<name>" will use the substring matched by the group named "name", and "\g<number>" uses the corresponding group number. "\g<2>" is therefore equivalent to "\2", but isn't ambiguous in a replacement string such as "\g<2>0". ("\20" would be interpreted as a reference to group 20, not a reference to group 2 followed by the literal character "0".) The following substitutions are all equivalent, but use all three variations of the replacement string.
?
>>> p = re.compile('section{ (?P<name> [^}]* ) }', re.VERBOSE) >>> p.sub(r'subsection{\1}','section{First}') 'subsection{First}' >>> p.sub(r'subsection{\g<1>}','section{First}') 'subsection{First}' >>> p.sub(r'subsection{\g<name>}','section{First}') 'subsection{First}'replacement can also be a function, which gives you even more control. If replacement is a function, the function is called for every non-overlapping occurrence of pattern. On each call, the function is passed a MatchObject argument for the match and can use this information to compute the desired replacement string and return it.
In the following example, the replacement function translates decimals into hexadecimal:
?
>>> def hexrepl( match ): ... "Return the hex string for a decimal number" ... value = int( match.group() ) ... return hex(value) ... >>> p = re.compile(r'\d+') >>> p.sub(hexrepl, 'Call 65490 for printing, 49152 for user code.') 'Call 0xffd2 for printing, 0xc000 for user code.'When using the module-level re.sub() function, the pattern is passed as the first argument. The pattern may be a string or a RegexObject; if you need to specify regular expression flags, you must either use a RegexObject as the first parameter, or use embedded modifiers in the pattern, e.g. sub("(?i)b+", "x", "bbbb BBBB") returns 'x x'.
?
6 Common Problems
Regular expressions are a powerful tool for some applications, but in some ways their behaviour isn't intuitive and at times they don't behave the way you may expect them to. This section will point out some of the most common pitfalls.
?
6.1 Use String Methods
Sometimes using the re module is a mistake. If you're matching a fixed string, or a single character class, and you're not using any re features such as the IGNORECASE flag, then the full power of regular expressions may not be required. Strings have several methods for performing operations with fixed strings and they're usually much faster, because the implementation is a single small C loop that's been optimized for the purpose, instead of the large, more generalized regular expression engine.
One example might be replacing a single fixed string with another one; for example, you might replace "word"with "deed". re.sub() seems like the function to use for this, but consider the replace() method. Note that replace() will also replace "word" inside words, turning "swordfish" into "sdeedfish", but the na?ve RE word would have done that, too. (To avoid performing the substitution on parts of words, the pattern would have to be \bword\b, in order to require that "word" have a word boundary on either side. This takes the job beyond replace's abilities.)
Another common task is deleting every occurrence of a single character from a string or replacing it with another single character. You might do this with something like re.sub('\n', ' ', S), but translate() is capable of doing both tasks and will be faster that any regular expression operation can be.
In short, before turning to the re module, consider whether your problem can be solved with a faster and simpler string method.
?
6.2 match() versus search()
The match() function only checks if the RE matches at the beginning of the string while search() will scan forward through the string for a match. It's important to keep this distinction in mind. Remember, match() will only report a successful match which will start at 0; if the match wouldn't start at zero, match() will not report it.
?
>>> print re.match('super', 'superstition').span() (0, 5) >>> print re.match('super', 'insuperable') NoneOn the other hand, search() will scan forward through the string, reporting the first match it finds.
?
>>> print re.search('super', 'superstition').span() (0, 5) >>> print re.search('super', 'insuperable').span() (2, 7)Sometimes you'll be tempted to keep using re.match(), and just add .* to the front of your RE. Resist this temptation and use re.search() instead. The regular expression compiler does some analysis of REs in order to speed up the process of looking for a match. One such analysis figures out what the first character of a match must be; for example, a pattern starting with Crow must match starting with a "C". The analysis lets the engine quickly scan through the string looking for the starting character, only trying the full match if a "C" is found.
Adding .* defeats this optimization, requiring scanning to the end of the string and then backtracking to find a match for the rest of the RE. Use re.search() instead.
?
6.3 Greedy versus Non-Greedy
When repeating a regular expression, as in a*, the resulting action is to consume as much of the pattern as possible. This fact often bites you when you're trying to match a pair of balanced delimiters, such as the angle brackets surrounding an HTML tag. The na?ve pattern for matching a single HTML tag doesn't work because of the greedy nature of .*.
?
>>> s = '<html><head><title>Title</title>' >>> len(s) 32 >>> print re.match('<.*>', s).span() (0, 32) >>> print re.match('<.*>', s).group() <html><head><title>Title</title>The RE matches the "<" in "<html>", and the .* consumes the rest of the string. There's still more left in the RE, though, and the > can't match at the end of the string, so the regular expression engine has to backtrack character by character until it finds a match for the >. The final match extends from the "<" in "<html>"to the ">" in "</title>", which isn't what you want.
In this case, the solution is to use the non-greedy qualifiers *?, +?, ??, or {m,n}?, which match as little text as possible. In the above example, the ">" is tried immediately after the first "<" matches, and when it fails, the engine advances a character at a time, retrying the ">" at every step. This produces just the right result:
?
>>> print re.match('<.*?>', s).group() <html>(Note that parsing HTML or XML with regular expressions is painful. Quick-and-dirty patterns will handle common cases, but HTML and XML have special cases that will break the obvious regular expression; by the time you've written a regular expression that handles all of the possible cases, the patterns will be very complicated. Use an HTML or XML parser module for such tasks.)
?
6.4 Not Using re.VERBOSE
By now you've probably noticed that regular expressions are a very compact notation, but they're not terribly readable. REs of moderate complexity can become lengthy collections of backslashes, parentheses, and metacharacters, making them difficult to read and understand.
For such REs, specifying the re.VERBOSE flag when compiling the regular expression can be helpful, because it allows you to format the regular expression more clearly.
The re.VERBOSE flag has several effects. Whitespace in the regular expression that isn't inside a character class is ignored. This means that an expression such as dog | cat is equivalent to the less readable dog|cat, but [a b] will still match the characters "a", "b", or a space. In addition, you can also put comments inside a RE; comments extend from a "#" character to the next newline. When used with triple-quoted strings, this enables REs to be formatted more neatly:
?
pat = re.compile(r""" \s* # Skip leading whitespace (?P<header>[^:]+) # Header name \s* : # Whitespace, and a colon (?P<value>.*?) # The header's value -- *? used to # lose the following trailing whitespace \s*$ # Trailing whitespace to end-of-line """, re.VERBOSE)This is far more readable than:
?
pat = re.compile(r"\s*(?P<header>[^:]+)\s*:(?P<value>.*?)\s*$")?
7 Feedback
Regular expressions are a complicated topic. Did this document help you understand them? Were there parts that were unclear, or Problems you encountered that weren't covered here? If so, please send suggestions for improvements to the author.
The most complete book on regular expressions is almost certainly Jeffrey Friedl's Mastering Regular Expressions, published by O'Reilly. Unfortunately, it exclusively concentrates on Perl and Java's flavours of regular expressions, and doesn't contain any Python material at all, so it won't be useful as a reference for programming in Python. (The first edition covered Python's now-obsolete regex module, which won't help you much.) Consider checking it out from your library.
?
About this document ...
Regular Expression HOWTO
This document was generated using the LaTeX2HTML translator.
LaTeX2HTML is Copyright ? 1993, 1994, 1995, 1996, 1997, Nikos Drakos, Computer Based Learning Unit, University of Leeds, and Copyright ? 1997, 1998, Ross Moore, Mathematics Department, Macquarie University, Sydney.
The application of LaTeX2HTML to the Python documentation has been heavily tailored by Fred L. Drake, Jr. Original navigation icons were contributed by Christopher Petrilli.
本文引用通告地址:http://www.sace.cn/members/persons/lpjcom/blog/20061011_115045_788/doTrackback
總結
以上是生活随笔為你收集整理的Python正则表达式操作指南的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: wifi卡慢延迟高_家里WIFI越用越卡
- 下一篇: 低级语言和高级语言的区别