@@ -48,7 +48,7 @@ .. _re-syntax: -Regular Expression Syntax +Regular expression syntax ------------------------- A regular expression (or RE) specifies a set of strings that matches it; the @@ -80,7 +80,7 @@ characters either stand for classes of ordinary characters, or affect how the regular expressions around them are interpreted. Regular expression pattern strings may not contain null bytes, but can specify -the null byte using the ``\number`` notation, e.g., ``'\x00'``. +the null byte using the ``\number`` notation, e.g. ``'\x00'``. The special characters are: @@ -405,7 +405,7 @@ \r \t \v \x \\ -Octal escapes are included in a limited form: If the first digit is a 0, or if +Octal escapes are included in a limited form. If the first digit is a 0, or if there are three octal digits, it is considered an octal escape. Otherwise, it is a group reference. As for string literals, octal escapes are always at most three digits in length. @@ -413,7 +413,7 @@ .. _matching-searching: -Matching vs Searching +Matching vs. searching --------------------- .. sectionauthor:: Fred L. Drake, Jr. @@ -438,7 +438,7 @@ .. _contents-of-module-re: -Module Contents +Module contents --------------- The module defines several functions, constants, and an exception. Some of the @@ -595,8 +595,8 @@ ['', '...', 'words', ', ', 'words', '...', ''] That way, separator components are always found at the same relative - indices within the result list (e.g., if there's one capturing group - in the separator, the 0th, the 2nd and so forth). + indices within the result list (e.g. if there's one capturing group + in the separator, the 0th, the 2nd, and so forth). Note that *split* will never split a string on an empty pattern match. For example: @@ -689,7 +689,7 @@ .. function:: escape(string) - Escape all the characters in pattern except ASCII letters, numbers and ``'_'``. + Escape all the characters in pattern except ASCII letters, numbers, and ``'_'``. This is useful if you want to match an arbitrary literal string that may have regular expression metacharacters in it. @@ -712,11 +712,11 @@ .. _re-objects: -Regular Expression Objects +Regular expression objects -------------------------- Compiled regular expression objects support the following methods and -attributes. +attributes: .. method:: regex.search(string[, pos[, endpos]]) @@ -735,7 +735,7 @@ The optional parameter *endpos* limits how far the string will be searched; it will be as if the string is *endpos* characters long, so only the characters from *pos* to ``endpos - 1`` will be searched for a match. If *endpos* is less - than *pos*, no match will be found, otherwise, if *rx* is a compiled regular + than *pos*, no match will be found; otherwise, if *rx* is a compiled regular expression object, ``rx.search(string, 0, 50)`` is equivalent to ``rx.search(string[:50], 0)``. @@ -820,11 +820,11 @@ .. _match-objects: -Match Objects +Match objects ------------- Match objects always have a boolean value of :const:`True`, so that you can test -whether e.g. :func:`match` resulted in a match with a simple if statement. They +whether, e.g., :func:`match` resulted in a match with a simple if statement. They support the following methods and attributes: @@ -997,11 +997,11 @@ .. _re-examples: -Regular Expression Examples +Regular expression examples --------------------------- -Checking For a Pair +Checking for a pair ^^^^^^^^^^^^^^^^^^^ In this example, we'll use the following helper function to display match @@ -1109,7 +1109,7 @@ If you create regular expressions that require the engine to perform a lot of recursion, you may encounter a :exc:`RuntimeError` exception with the message -``maximum recursion limit`` exceeded. For example, :: +``maximum recursion limit exceeded``. For example: :: >>> s = 'Begin ' + 1000*'a very long string ' + 'end' >>> re.match('Begin (\w| )*? end', s).end() @@ -1159,13 +1159,13 @@ >>> pattern.match("dog", 2) # No match as "o" is not the 3rd character of "dog." -Making a Phonebook +Making a phone book ^^^^^^^^^^^^^^^^^^ :func:`split` splits a string into a list delimited by the passed pattern. The method is invaluable for converting textual data into data structures that can be easily read and modified by Python as demonstrated in the following example that -creates a phonebook. +creates a phone book. First, here is the input. Normally it may come from a file, here we are using triple-quoted string syntax: @@ -1218,7 +1218,7 @@ ['Heather', 'Albrecht', '548.326.4584', '919', 'Park Place']] -Text Munging +Text munging ^^^^^^^^^^^^ :func:`sub` replaces every occurrence of a pattern with a string or the @@ -1237,7 +1237,7 @@ 'Pofsroser Aodlambelk, plasee reoprt yuor asnebces potlmrpy.' -Finding all Adverbs +Finding all adverbs ^^^^^^^^^^^^^^^^^^^ :func:`findall` matches *all* occurrences of a pattern, not just the first @@ -1250,7 +1250,7 @@ ['carefully', 'quickly'] -Finding all Adverbs and their Positions +Finding all adverbs and their positions ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ If one wants more information about all matches of a pattern than the matched @@ -1266,7 +1266,7 @@ 40-47: quickly -Raw String Notation +Raw string notation ^^^^^^^^^^^^^^^^^^^ Raw string notation (``r"text"``) keeps regular expressions sane. Without it, @@ -1290,7 +1290,7 @@ <_sre.SRE_Match object at ...> -Writing a Tokenizer +Writing a tokenizer ^^^^^^^^^^^^^^^^^^^ A `tokenizer or scanner `_