


A more detailed guide to Python regular expression operations
Python has added the re module since version 1.5, which provides Perl-style regular expression patterns. Versions before Python 1.5 provided Emecs-style patterns through the regex module. Emacs style mode is slightly less readable and less powerful, so try not to use the regex module when writing new code. Of course, occasionally you may still find traces of it in old code.
By its very nature, regular expressions (or RE) are a small, highly specialized programming language embedded in Python and implemented through the re module. Using this small language, you can specify rules for a corresponding set of strings that you want to match; that set of strings might contain English sentences, e-mail addresses, TeX commands, or whatever you want. You can then ask questions such as "Does this string match the pattern?" or "Does any part of this string match the pattern?". You can also use RE to modify or split strings in various ways.
Regular expression patterns are compiled into a series of bytecodes and then executed by a matching engine written in C. In advanced usage, one might also want to pay careful attention to how the engine executes a given RE, and how the RE is written in a specific way to make the produced bytecode run faster. This article does not cover optimization, as that requires you to fully understand the internal mechanisms of the matching engine.
The regular expression language is relatively small and restricted (limited functionality), so not all string processing can be done with regular expressions. Of course, there are some tasks that can be accomplished with regular expressions, but in the end the expressions will become extremely complex. When you encounter these situations, it may be better to write Python code to deal with it; although Python code is slower than a fancy regular expression, it is easier to understand.
Simple Mode
We will start with the simplest regular expression learning. Since regular expressions are often used for string manipulation, let's start with the most common task: character matching.
For a detailed explanation of the computer science underlying regular expressions (deterministic and non-deterministic finite automata), you can consult any textbook on writing compilers.
Character matching
Most letters and characters will generally match themselves. For example, the regular expression test will exactly match the string "test". (You can also use case-insensitive mode, which will also make this RE match "Test" or "TEST"; more on that later.)
There are of course exceptions to this rule; some characters Rather special, they do not match themselves, but indicate that they should match something special, or they will affect the number of repetitions of other parts of the RE. A large section of this article is devoted to discussing various metacharacters and their functions.
Here is a complete list of metacharacters; their meanings are discussed in the remainder of this guide.
. ^ $ * + ? { [ ] \ | ( )
The metacharacters we first examine are "[" and "]". They are often used to specify a character category, which is a character set you want to match. Characters can be listed individually, or two given characters separated by a "-" sign can be used to represent a character range. For example, [abc] will match any character among "a", "b", or "c"; you can also use the interval [a-c] to represent the same character set, which has the same effect as the former. If you only want to match lowercase letters, then RE should be written as [a-z]. The
metacharacter has no effect in categories. For example, [akm$] will match any of the characters "a", "k", "m", or "$"; "$" is usually used as a metacharacter, but within the character class, its characteristics are removed, Revert to normal characters.
You can use complement to match characters that are not within the range. The method is to use "^" as the first character of the category; "^" elsewhere will simply match the "^" character itself. For example, [^5] will match any character except "5".
Perhaps the most important metacharacter is the backslash """. As a string letter in Python, different characters can be added after the backslash to express different special meanings. It can also be used to cancel all metacharacters. characters so that you can match them in a pattern. For example, if you need to match the characters "[" or """, you can precede them with a backslash to remove their special meaning: "[ or " ".
Some predefined character sets represented by special characters starting with """ are usually useful, such as numeric sets, alphabetic sets, or other non-empty character sets. The following are the preset special characters available:
#!python
#>>> import re
##>>> p = re.compile( 'ab*')
##>>>
print p##<
re.RegexObject instance at 80b4150>re.compile() also accepts optional flag parameters, which are often used to implement different special functions and syntax changes. We'll look at all the available settings later, but for now just one example:
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com /
#!python
##>> > p
= re.compile('ab*', re.IGNORECASE)
RE is sent to re.compile() as a string. REs are processed as strings because regular expressions are not a core part of the Python language, and no specific syntax was created for them. (Applications simply don't need REs, so there's no need to bloat the language specification by including them.) The re module is simply included in Python as a C extension module, just like the socket or zlib modules.
Use REs as strings to ensure the simplicity of the Python language, but this brings about a trouble as mentioned in the title of the next section.
The trouble with backslashes
In earlier regulations, regular expressions used the backslash character (""") to indicate special formatting or to allow the use of a special character without calling its special Usage. This conflicts with Python's use of identical characters in strings.
Let's say you want to write an RE to match the string ""section". Probably looking in a LATEX file. In order to judge in program code, first write the string you want to match. Next you need to precede all backslashes and metacharacters with a backslash to remove their special meaning.
Characters | Phase |
\section | String to match |
\\section | Cancel the special meaning of backslash for re.compile |
"\\\\section" | Cancel backslashes for strings |
# Simply put, in order to match a backslash, you have to write '\ in the RE string \', because the regular expression must be "\\", and each backslash must be represented as "\\" according to the conventional Python string letter representation. This repetitive nature of backslashes in REs results in a large number of repeated backslashes, and the resulting strings are difficult to understand.
The solution is to use Python's raw string representation for regular expressions; adding an "r" backslash before the string will not be processed in any special way, so r"\ n" is two characters containing "\" and "n", and "\n" is one character, representing a newline. Regular expressions are usually represented by this raw string in Python code.
Regular string | Raw string |
"ab*" | r "ab*" |
"\\\\section" | r"\\section" |
"\\ w+\\s+\\1" | r"\w+\s+\1" |
Perform the match
Once you have Compiled regular expression object, what do you want to do with it? `RegexObject` instances have some methods and properties. Only the most important ones are shown here. If you want to see the complete list, please consult the Python Library Reference
Methods/Attributes | Role |
match() | Determine whether RE matches at the beginning of the string |
search() | Scan String, find the position matched by this RE |
findall() | Find all substrings matched by RE and return them as a list |
finditer() | Find all substrings matched by RE and return them as an iterator |
If no match is found, match() and search() will return None. If successful, a `MatchObject` instance will be returned with information about the match: where it starts and ends, the substring it matched, etc.
You can learn it by using human-computer dialogue and experimenting with the re module. If you have Tkinter, you might consider looking at Tools/scripts/redemo.py, a demo program included with the Python distribution.
First, run the Python interpreter, import the re module and compile a RE:
#!python
##Python
2.2.2 (1, Feb 10 2003, 12:57:01)
#>>> import re
>>> p = re.compile(' [a-z]+')
>>> p
<_sre.SRE_Pattern object at 80c3c28>
Code highlighting produced by Actipro CodeHighlighter (freeware)
#!python
#>>>
p.match("")##>>>
print p.match ("")None
Now, let us try to use it to match a string like " tempo". In this case, match() will return a MatchObject. So you can save the result in a variable for later use.
# !python
#>>> m = p.match( 'tempo')
>> > print m
##<_sre.SRE_Match object at 80c4f68>
Now you can query `MatchObject` for related information about matching strings. MatchObject instances also have several methods and properties; the most important ones are as follows:
Try these methods and you will soon know what they do:
#!python
##> >> m.group()
'tempo'
>>> m.start(), m.end()
(0,
5)
>>> m.span()
(0,
5)
#!python
#>>> print p.match('::: message')None
>>> m = p.search('::: message') ; print m
##<
re.MatchObject instance at 80c9650>##>>>
m. group()'
message'##> >>
m.span()
(4
,
11) In actual programs, the most common approach is to save `MatchObject` in a variable and then check whether it is None, usually as follows:
# !python
##p
= re.compile( )
m
= p.match( 'string goes here' )
if m:
print 'Match found: ', m.group()
else:
print 'No match'
Two `RegexObject` methods Returns all substrings matching the pattern. findall() returns a list of matching string rows:
#!python
>>> p = re.compile('" d+')
##>>> p.findall(' 12 drummers drumming, 11 pipers piping, 10 lords a-leaping')[
' 12', '11', '10']findall() has to create a list when it returns a result in Python 2.2. , you can also use the finditer() method
# !python
#>>> iterator = p.finditer('12 drummers drumming, 11 10 ')
>>> iterator
##<callable-iterator object at 0x401833ac>
#>>>
for match in iterator:
print match.span()
(0,
2
)(
22
, 24)(
29
, 31)
Module-level functions
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
#!python
#>>>
print re.match(r'From"s+', 'Fromage amk')None
##>>>
re .match(r
'From"s+', 'From amk Thu May 14 19:12:10 1998')<
re.MatchObject instance at 80c5978
>
Under the hood, these functions simply generate a RegexOject and call the corresponding methods on it. They also store compiled objects in cache, so future calls using the same RE will be faster.
Will you use these module-level functions, or get a `RegexObject` first and then call its methods? Which one you choose depends on how efficient you are with RE and your personal coding style. If a RE is only used once in the code, then module-level functions may be more convenient. If the program contains a lot of regular expressions, or reuses the same one in multiple places, it is more useful to put all the definitions together and compile all REs ahead of time in a block of code. Take a look at an example from the standard library, extracted from the xmllib.py file:
#!python
##ref
= re.compile( )
entityref
= re.compile( )
charref
= re.compile( )
starttagopen
= re.compile( )
There is a table of available flags, with detailed instructions after each flag.
I
IGNORECASE
Make matching case-insensitive; character classes and strings ignore case when matching letters. For example, [A-Z] can also match lowercase letters, and Spam can match "Spam", "spam", or "spAM". This lowercase letter does not take into account the current position.
L
LOCALE
Affects "w, "W, "b, and "B, depending on the current localization set up.
locales is a feature in the C library that is used to assist programming where different languages need to be considered. For example, if you're working with French text, you want to use "w+" to match text, but "w only matches the character class [A-Za-z]; it doesn't match "é" or "ç". If your system is configured appropriately and the locale is set to French, an internal C function will tell the program that "é" should also be considered a letter. Using the LOCALE flag when compiling a regular expression will result in a compiled object that uses these C functions to handle "w"; this will be slower, but will also allow you to use "w+ to match French text.
M
MULTILINE
(^ and $ will not be interpreted at this time; they will be introduced in Section 4.1 .)
Use "^" to match only the beginning of the string, while $ will only match the end of the string and the end of the string immediately before the newline (if there is one). When this flag is specified, "^" matches the beginning of the string and the beginning of each line in the string. Likewise, the $ metacharacter matches the end of the string and the end of each line in the string (directly before each newline).
S
DOTALL
Causes the "." special character to match exactly any character, including newlines; without this flag, "." Matches any character except newline.
X
VERBOSE
This flag makes it easier to write regular expressions by giving you a more flexible format understand. When this flag is specified, whitespace within the RE string is ignored unless the whitespace is within a character class or after a backslash; this allows you to organize and indent REs more clearly. It also allows you to write comments to the RE, which will be ignored by the engine; comments are marked with a "#" symbol, but this symbol cannot come after a string or a backslash.
For example, here is a RE using re.VERBOSE; see how much easier it is to read it?
#!python
##charref
= re.compile(r"" "##&[[]] # Decimal form
| 0[0-7]+[^0-7] # Octal form
| x[0-9a-fA-F]+[^0-9a- fA-F] # Hexadecimal form
)
"""
, re.VERBOSE)
No verbose setting, The RE will look like this:
#!python
charref =
re.compile(
"([0-9]+[^0-9]" "|0[0-7]+[^0-7]
""|x[0-9a-fA-F]+[^0-9a-fA-F])
")In the above example, Python's string auto-concatenation can be used to break the RE into smaller parts, but it is more difficult to understand than when using the re.VERBOSE flag. More pattern functionsSo far, we have only shown part of the functions of regular expressions. In this section, we'll show you some new metacharacters and how to use groups to retrieve matched parts of text.
More Metacharacters
There are some metacharacters we haven’t shown yet, most of which will be shown in this section.
The remaining metacharacters to discuss are zero-width assertions. They don't make the engine faster when processing strings; rather, they don't correspond to any characters at all, just simple success or failure. For example, "b is an assertion that locates the current position at a word boundary, which position is not changed at all by "b. This means that zero-width assertions will never be repeated, because if they match once at a given position, they can obviously be matched an infinite number of times.
|
Optional, or "or" operator. If A and B are regular expressions, A|B will match any string that matches "A" or "B". | has a very low priority so that it behaves appropriately when you have multiple strings to select. Crow|Servo will match "Crow" or "Servo", but not "Cro", a "w" or an "S", and "ervo".
To match the letter "|", you can use "|, or include it in a character class, such as [|].
^
Matches the beginning of the line. Unless the MULTILINE flag is set, it only matches the beginning of the string. In MULTILINE mode, it can also directly match every newline in the string. #For example, if you only want to match the word "From" at the beginning of the line, then RE will use ^From
http: //www.CodeHighlighter.com/
#!python
##>>>
print re.search('^From ', 'From Here to Eternity')<
re.MatchObject instance at 80c1520>##>>>
print re.search('^From', 'Reciting From Memory')None
$
Matches the end of the line, which is defined as either the end of the string or any position after a newline character .
# !python
#>>> print re.search('}$', '{block} ')
##<re.MatchObject instance at 80adfa8>
##>>>
print re.search('}$', '{block} ' )None
##>>>
print re. search('}$', '{block}"n')##<
re.MatchObject instance at 80adfa8
> To match a "$", use "$" or include it in a character class, such as [$]. "A
Only matches the beginning of the string. When not in MULTILINE mode, "A and ^ are actually the same. However, they are different in MULTILINE mode; "A only matches the beginning of the string, while ^ can also match anywhere in the string after the newline character.
"Z
Matches only at the end of the string.
"b
The following example only matches the entire word "class"; not when it is contained in other words. match.
# !python
#>>> p = re.compile(r'"bclass"b')
##> ;>> print p.search('no class at all')
##<
re.MatchObject instance at 80c8f28>
>>>
print p.search(' the declassified algorithm')None
##>>>
print p.search('one subclass is')None
There are two subtleties here that you should keep in mind when using this particular sequence. The first is the worst conflict between Python strings and regular expressions. In Python strings, ""b" is a backslash character, and its ASCII value is 8. If you do not use a raw string, then Python will convert ""b" into a fallback character, and your RE You won't be able to match it as you would like. The following example looks the same as our previous RE, but without an "r" in front of the RE string.
# !python
#>>> p = re.compile('"bclass"b')
##> >> print p.search('no class at all ')None
##>>>
print p.search('"b' + 'class' + '" b')
<
re.MatchObject instance at 80c3ee0>The second one is in the character class. This qualifier (assertion) has no effect. "b represents the fallback character for compatibility with Python strings.
"B
Another zero-width assertion (zero-width assertions), which is just the opposite of "b, and only matches when the current position is not at a word boundary.
Grouping
Groups are identified by the "(" and ")" metacharacters. "(" and ")" have much the same meaning in mathematical expressions; they are used together in them. The expressions inside form a group. For example, you can use repetition qualifiers like *, +, ?, and {m,n} to repeat the contents of the group. For example, (ab)* will match zero or More repetitions of "ab".
http://www.CodeHighlighter.com/
#!python
#>>>
p = re.compile('(ab)*')>>>
print p.match('ababababab').span()(0, 10
)
Groups are specified with "(" and ")" and get the start and end indices of the text they match; this can be done with one argument using group(), start(), end() and span() Retrieve. Groups are counted starting from 0. Group 0 is always present; it is the entire RE, so the methods of `MatchObject` all take group 0 as their default argument. Later we'll see how to express spans that cannot get the text they match.
#!python
#>>> p = re.compile('(a)b')
>>> m = p.match('ab')
##>>> m.group()
'ab'
>>> m.group(0 )
'ab'
The panel is from the left Counting to the right, starting from 1. Groups can be nested. The numerical value of the count can be determined by counting the number of open brackets from left to right.
Code highlighting produced by Actipro CodeHighlighter (freeware)
#!python
#>>>
p = re.compile('(a(b)c)d')
>>>
m = p.match('abcd')##>>>
m.group(0)'
abcd'##>>>
m.group(
1)'
abc
'##>>> m.group(
2)'b
'group() You can enter multiple group numbers at one time, here In this case it will return a tuple containing the values corresponding to those groups.
# !python
#>>> m.group(2,1,2)
(
'b', 'abc', 'b')
The groups() method returns a tuple containing all group strings, starting from
1 to the group number contained in .
#!python
#>>> ; m.groups()(
'abc', 'b')
Backreferences in the pattern allow you to specify the contents of a previous capturing group, The group must also be found at the current position in the string. For example, if the content of group 1 can be found at the current location, "1 succeeds otherwise it fails. Remember that Python strings also use backslashes to add data to allow the string to contain any characters, so when in RE Make sure to use raw strings when using backreferences.
## Code highlighting produced. by Actipro CodeHighlighter (freeware)
#!python
##>>> p
= re.compile(r'("b"w+)"s+"1')##>>> p.search(
'Paris in the the spring').group()'the the
'It's not common to just search for a backreference to a string like this - use this Text formats that repeat data in this way are rare -- but you'll soon find them useful for string replacement. No capturing groups and named groupsCarefully designed. REs may use many groups, both to capture substrings of interest and to group and structure the RE itself. In complex REs, keeping track of group numbers becomes difficult. Two features can help with this problem. They also both use a common syntax for regular expression expansion, so let's look at the first one.
Perl 5 adds several additional features to standard regular expressions, most of which are supported by Python's re module. It can be difficult to choose a new single-key metacharacter or a special sequence starting with """ to represent new functionality without confusing Perl regular expressions with standard regular expressions. If you choose "& " As a new metacharacter, for example, old expressions consider "&" to be a normal character and will not escape it when using "& or [&].
The solution for Perl developers is to use (?...) as the extension syntax. "?" directly after the bracket will cause a syntax error, because "?" does not have any characters to repeat, so it will not create any compatibility issues. The character immediately following "?" indicates the purpose of the extension, so (?=foo)
Python has added an extension syntax to the Perl extension syntax. You can tell it's an extension for Python if the first character after the question mark is "P". There are currently two such extensions: (?P
Now that we look at the normal extension syntax, we go back to simplifying the features of using group operations in complex REs. Because groups are numbered from left to right, and a complex expression may use many groups, it can make it difficult to keep track of the current group number, and modifying such a complex RE can be cumbersome. Inserts a new group at the beginning and you can change each group number after it.
First of all, sometimes you want to use a group to collect part of the regular expression, but you are not interested in the content of the group. You can achieve this functionality with a non-capturing group: (?:...) so that you can send any other regular expression in parentheses.
#!python
#>>> m = re.match("([abc])+", "abc")
>>> m.groups()
(
'c',)
##>> > m = re.match("(?:[abc])+ ", "abc")
>>> m.groups()()
In addition to capturing the contents of the matching group, there are no capturing groups and capturing groups It behaves exactly the same; you can put any characters in it, you can repeat it with repeating metacharacters like "*", you can nest it in other groups (non-capturing groups and capturing groups). (?:...) is especially useful for modifying existing groups, since you can add a new group without changing all other group numbers. There is also no difference in search efficiency between capturing and non-capturing groups, neither one is faster than the other.
The syntax of the command group is one of the Python-specific extensions: (?P
#!python
## >>> p = re.compile(r'(?P
>>> m = p.search( '(((Lots of punctuation)))' )
>>> m.group('word')
'Lots'
>>> m.group(1)
##' Lots'Named groups are convenient because they allow you to use easy-to-remember names instead of having to remember numbers. Here's one from the imaplib module. RE example:
Code highlighting produced by Actipro CodeHighlighter (freeware)
#!python
##InternalDate =
re.compile(r'INTERNALDATE "'r
'
(?P
'
(?P
'
(?P
(?P
"')Obviously, getting m.group('zonem') is much easier than remembering to get group 9.
Because of the reverse reference syntax, expressions like (...)"1 represent the group number. In this case, there will naturally be a difference if the group name is used instead of the group number. There is also a Python extension: (?P=name), which can make the group content named name found again at the current position. In order to find repeated words, the regular expression ("b"w+)"s+"1 can also be written as (?P
#!python
#>>> p = re.compile(r'(?P
>>> p.search('Paris in the the spring').group()
' the the'
(?=...)
Forward positive delimiter symbol. If the contained regular expression, denoted by... , succeeds at the current position, otherwise it fails, but once the contained expression has been tried, the matching engine does not improve at all; the remainder of the pattern still has to be defined. The right side of the delimiter.(?!...)
The opposite of the positive delimiter; when the contained expression cannot be in the string. Succeed when current position matches It helps to demonstrate where forwarding can succeed. Consider a simple pattern that matches a filename and splits it into a basename and a "." There are two parts to the extension. For example, in "news.rc", "news" is the base name and "rc" is the file extension.
The matching pattern is very simple:
.*[.].*$
. *[.][^b].*$
The first attempt above to remove "bat" requires that the first character of the extension be something other than "b". This is wrong because the pattern cannot match "foo.bar" either.
. *[.]([^b]..|.[ ^a].|..[^t])$
The expression gets even messier when you try to patch the first solution to require matching one of the following: the first character of the extension is not "b"; the second character is not "a"; or The third character is not "t". This would accept "foo.bar" but reject "autoexec.bat", but would only require a three-character extension and would not accept a two-character extension such as "sendmail.cf". We're going to complicate the pattern again as we work on patching it.
. *[.]([^b].?.?|.[^a]?.?|..?[^t]? )$
In the third attempt, both the second and third letters were made optional to allow matching extensions shorter than three characters, such as "sendmail. cf".
The schema is now very complex, which makes it hard to read. Worse, if the problem changes and you want extensions other than "bat" and "exe", the pattern becomes even more complicated and confusing.
Forward negation clips all of this to:
.*[.](?!bat$).*$
Forward meaning: If the expression bat does not match here, try the rest of the pattern; if bat$ matches, the entire pattern will fail. The trailing $ is required to ensure that extensions starting with "bat" like "sample.batch" are allowed.
It is now also easy to exclude another file extension; simply make it optional in the delimiter. The pattern below will exclude filenames ending in "bat" or "exe".
. *[.](?!bat$|exe$).*$
Modify String
So far, we have simply searched for a static string. Regular expressions are also usually used in different ways to modify strings through the `RegexObject` method below.
Method/Property | Function |
split() | Put the string in Slice where RE matches and generate a list, |
sub() | Find all substrings matched by RE and replace them with a different string |
subn() | Same as sub(), but returns the new string and the number of substitutions |
Split the string
The split() method of `RegexObject` splits the string where RE matches and will return a list. It is similar to the string split() method but provides more delimiters; split() only supports whitespace and fixed strings. As you might expect, there is also a module-level re.split() function.
split(string [ , maxsplit = 0])
Split the string through regular expressions. If capturing brackets are used in a RE, their contents are also returned as part of the result list. If maxsplit is non-zero, then at most maxsplit shards can be split.
You can limit the number of splits by setting the maxsplit value. When maxsplit is non-zero, there can be at most maxsplit splits, and the remainder of the string is returned as the last part of the list. In the following examples, the delimiter can be any sequence of non-alphanumeric characters.
#!python
>>> 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().']
有时,你不仅对定界符之间的文本感兴趣,也需要知道定界符是什么。如果捕获括号在 RE 中使用,那么它们的值也会当作列表的一部分返回。比较下面的调用:
# !python
#>>> 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() takes RE as the first parameter, and the others are the same.
# !python
#>>> 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.']
Search and Replace
Other common uses are to find all patterns Matches strings and replaces them with different strings. The sub() method provides a replacement value, which can be a string or a function, and a string to be processed.
Code highlighting produced by Actipro CodeHighlighter (freeware)
sub(replacement, string[, count
= 0])
The returned string is replaced with the leftmost non-repeating match of RE in the string. If the pattern is not found, the character will be returned unchanged.
The optional parameter count is the maximum number of substitutions after pattern matching; count must be a non-negative integer. The default value is 0 which replaces all matches.
Here is a simple example using the sub() method. It replaces the color name with the word "colour".
#!python
#>>> p = re.compile( '(blue|white|red)')
>>> p.sub( 'colour', 'blue socks and red shoes')
' color socks and color shoes'
##>>> p.sub( 'colour', 'blue socks and red shoes', count=1)
'colour socks and red shoes'
subn() method has the same effect, but returns a two-tuple containing the new string and the number of replacement execution times.
# !python
#>>> p = re.compile( '(blue|white|red)')
>>> p.subn( 'colour', 'blue socks and red shoes')
(
'colour socks and color shoes', 2)
##>>> p.subn( 'colour', ' no colors at all')(
'no colors at all', 0)
Empty matches will only be replaced if they are not immediately following a previous match.
Code highlighting produced by Actipro CodeHighlighter (freeware)
#!python
#>>>
p = re.compile('x*')## >>>
p.sub('-', 'abxd')'
-a-b-d- '#If the replacement is a string, any backslashes in it will be processed. ""n" will be converted to a newline character, ""r" to a carriage return, etc. Unknown escapes such as ""j" are left intact. Backreferences, such as ""6", are matched by the corresponding group in the RE and replaced by the substring. This allows you to insert part of the original text into the replaced string.
# !python
#>>> p = re.compile('section{ ( [^}]* ) }', re.VERBOSE)
>>> p.sub(r'subsection{"1} ','section{First} section{second}')
'subsection{First} subsection{second}'
# !python
#>>> p = re.compile('section{ (?P
>>> p.sub(r'subsection{"1}','section{First}')
'subsection{First}'
#>>> p.sub(r'subsection{"g<1>}', 'section{First}')
'subsection{First}'
##>>>
p.sub(r' subsection{"g
'
subsection{First}'##The replacement can also be a function which gives you even more control. If the replacement is a function, the function will be called for every unique match in the pattern, and the function will be called as a `MatchObject. ` matching function, and can use this information to calculate the expected string and return it
# !python
#>>> 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 You must either use `RegexObject` as the first argument, or use the modifier inline using the pattern, such as sub("(?i)b+", "x", "bbbb BBBB") returns 'x x'. FAQ
Regular expressions are a powerful tool for some applications, but sometimes they are not intuitive and sometimes they don't work as you expect. This section will point out some of the most common ones. Common mistakes that are easy to make.
Using the string method
Sometimes using the re module is a mistake. If you are matching a fixed string or a single character class, and you are not using any of re. As the IGNORECASE flag does, there is no need to use regular expressions. Strings have some methods that operate on fixed strings. They are usually much faster because they are small optimized C loops that replace larger, more general regular expression engines.
Give an example of replacing one fixed string with another; for example, you can replace "deed" with "word". re.sub() seems like the function to use for this, but consider the replace() method. Note that replace() can also replace words, such as "swordfish" into "sdeedfish", but RE can also be used. Arrived. (To avoid replacing part of a word, the pattern would be written "bword"b, which requires a word boundary on either side of "word". This is a job beyond the capabilities of replacement).Another common task is to remove a single character from a string or replace it with another character. You could probably do it with something like re.sub('"n',' ',S), but translate() does both tasks and is faster than any regular expression.
In short, before using the re module, consider whether your problem can be solved with a faster and simpler string method
.
match() vs search()
The match() function only checks whether the RE matches at the beginning of the string, while search() scans the entire string. It's important to remember this distinction. Remember, match() will only report a successful match, which will start at 0; if the match does not start at 0, match() will not report it.
#!python
#>>> print re.match('super', 'superstition ').span()
(0,
5)
>>> print re.match('super', 'insuperable')
None
#!python
#>>> print re.search('super', 'superstition ').span()(0,
5)
>>> print re.search('super', 'insuperable').span()(
2, 7)Sometimes you may be inclined to use re.match(), only when Add .* to the front part of RE. Please try not to do this and use re.search() instead. The regular expression compiler does some analysis on REs to speed up processing when finding matches. One such parser will indicate what the first character of the match is; for example, the pattern Crow must match starting at "C". The parser allows the engine to quickly scan the string to find the starting character, and only start matching all after "C" is found.
Adding .* will cause this optimization to fail, which requires scanning to the end of the string and then backtracking to find a match for the remainder of the RE. Use re.search() instead.
Greedy vs Not Greedy
When repeating a regular expression, such as using a*, the result of the operation is to match as many patterns as possible. This fact often bothers you when you try to match a symmetrical pair of delimiters, such as angle brackets in HTML markup. Patterns matching a single HTML tag do not work properly because of the "greedy" nature of .*
#!python
##> ;>> s = '
>>> len(s)
32
#>>> print re.match('<.*>', s).span()(0,
32)
>>> print re.match('<.*>', s).group()
<html><head><title>Titletitle>
RE matches "<" in "
<span style="font-family:新宋体">", .* consumes the remaining part of the substring. Keeping more left in RE, though > cannot match at the end of the string, so the regex has to backtrack character by character until it finds a match for >. The final match goes from "<" in "<html" to ">" in "</title>", which is not what you want. </span>
Code highlighting produced by Actipro CodeHighlighter (freeware)#!python
##pat
= re.compile(r"""##"s* # Skip leading whitespace
(?P
"s* : # Whitespace, and a colon
(?P
# lose the following trailing whitespace
"s*$ # Trailing whitespace to end-of-line
""" , re.VERBOSE)This one is much harder to read:
Code highlighting produced by Actipro CodeHighlighter (freeware)
#!python
##pat =
re.compile(r""s*(?P <header>[^:]+)"s*:(?P
The above is the detailed content of A more detailed guide to Python regular expression operations. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

To run Python code in Sublime Text, you need to install the Python plug-in first, then create a .py file and write the code, and finally press Ctrl B to run the code, and the output will be displayed in the console.

Writing code in Visual Studio Code (VSCode) is simple and easy to use. Just install VSCode, create a project, select a language, create a file, write code, save and run it. The advantages of VSCode include cross-platform, free and open source, powerful features, rich extensions, and lightweight and fast.

VS Code can run on Windows 8, but the experience may not be great. First make sure the system has been updated to the latest patch, then download the VS Code installation package that matches the system architecture and install it as prompted. After installation, be aware that some extensions may be incompatible with Windows 8 and need to look for alternative extensions or use newer Windows systems in a virtual machine. Install the necessary extensions to check whether they work properly. Although VS Code is feasible on Windows 8, it is recommended to upgrade to a newer Windows system for a better development experience and security.

Running Python code in Notepad requires the Python executable and NppExec plug-in to be installed. After installing Python and adding PATH to it, configure the command "python" and the parameter "{CURRENT_DIRECTORY}{FILE_NAME}" in the NppExec plug-in to run Python code in Notepad through the shortcut key "F6".
