欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

python正则表达式的用法

程序员文章站 2024-02-08 15:48:28
[python] view plaincopyprint? import re  r1 = re.compile(r'(?im)(?p


[python] view plaincopyprint?
import re 
r1 = re.compile(r'(?im)(?p<name></html>)$') 
content = """
        <html>
boxsuch as 'box' and 'boxes', but not 'inbox'. in other words
box
<html>dsafdsafdas  </html> </ahtml>
</html>
  </html> 
""" 
 
reobj = re.compile("(?im)(?p<name></.*?html>)$") 
for match in reobj.finditer(content): 
    # match start: match.start() 
    # match end (exclusive): match.end() 
    # matched text: match.group() 
    print "start>>", match.start()   
    print "end>>", match.end() 
    print "span>>", match.span() 
    print "match.group()>>", match.group() 
 
print "*"*20 
 
     
 
if r1.match(content): print 'match succeeds' 
else: print 'match fails'                          # prints: match fails 
 
if r1.search(content): print 'search succeeds'   # prints: search succeeds 
else: print 'search fails' 
 
print r1.flags  
print r1.groupindex 
print r1.pattern 
 
l = r1.split(content) 
print "l>>", l 
 
for item in r1.findall(content): 
    print "item>>", item 
     
s = r1.sub("aa", content) 
print "s>>", s 
 
s_subn, s_sub_count = r1.subn("aaaaaaaaaaaa", content) 
print "s_subn>>", s_subn 
print "s_sub_count>>", s_sub_count 


[ team lib ] 
9.7 regular expressions and the re module
a regular expression is a string that represents a pattern. with regular expression functionality, you can compare that pattern to another string and see if any part of the string matches the pattern.
the re module supplies all of python's regular expression functionality. the compile function builds a regular expression object from a pattern string and optional flags. the methods of a regular expression object look for matches of the regular expression in a string and/or perform substitutions. module re also exposes functions equivalent to a regular expression's methods, but with the regular expression's pattern string as their first argument.
regular expressions can be difficult to master, and this book does not purport to teach them桰 cover only the ways in which you can use them in python. for general coverage of regular expressions, i recommend the book mastering regular expressions, by jeffrey friedl (o'reilly). friedl's book offers thorough coverage of regular expressions at both the tutorial and advanced levels.
9.7.1 pattern-string syntax
the pattern string representing a regular expression follows a specific syntax:
alphabetic and numeric characters stand for themselves. a regular expression whose pattern is a string of letters and digits matches the same string.
many alphanumeric characters acquire special meaning in a pattern when they are preceded by a backslash (\).
punctuation works the other way around. a punctuation character is self-matching when escaped, and has a special meaning when unescaped.
the backslash character itself is matched by a repeated backslash (i.e., the pattern \\).
since regular expression patterns often contain backslashes, you generally want to specify them using raw-string syntax (covered in chapter 4). pattern elements (e.g., r'\t', which is equivalent to the non-raw string literal '\\t') do match the corresponding special characters (e.g., the tab character '\t'). therefore, you can use raw-string syntax even when you do need a literal match for some such special character.
table 9-2 lists the special elements in regular expression pattern syntax. the exact meanings of some pattern elements change when you use optional flags, together with the pattern string, to build the regular expression object. the optional flags are covered later in this chapter.
table 9-2. regular expression pattern syntax

element
meaning
 .
matches any character except \n (if dotall, also matches \n)
 ^
matches start of string (if multiline, also matches after \n)
 $
matches end of string (if multiline, also matches before \n)
 *
matches zero or more cases of the previous regular expression; greedy (match as many as possible)
 +
matches one or more cases of the previous regular expression; greedy (match as many as possible)
 ?
matches zero or one case of the previous regular expression; greedy (match one if possible)
*? , +?, ??
non-greedy versions of *, +, and ? (match as few as possible)
{m,n}
matches m to n cases of the previous regular expression (greedy)
{m,n}?
matches m to n cases of the previous regular expression (non-greedy)
 [...]
matches any one of a set of characters contained within the brackets
 |
matches expression either preceding it or following it
 (...)
matches the regular expression within the parentheses and also indicates a group
(?ilmsux)
alternate way to set optional flags; no effect on match
(?:...)
like (...), but does not indicate a group
(?p<id>...)
like (...), but the group also gets the name id
(?p=id)
matches whatever was previously matched by group named id
(?#...)
content of parentheses is just a comment; no effect on match
(?=...)
lookahead assertion; matches if regular expression ... matches what comes next, but does not consume any part of the string
(?!...)
negative lookahead assertion; matches if regular expression ... does not match what comes next, and does not consume any part of the string
(?<=...)
lookbehind assertion; matches if there is a match for regular expression ... ending at the current position (... must match a fixed length)
(?<!...)
negative lookbehind assertion; matches if there is no match for regular expression ... ending at the current position (... must match a fixed length)
\number
matches whatever was previously matched by group numbered number (groups are automatically numbered from 1 up to 99)
\a
matches an empty string, but only at the start of the whole string
\b
matches an empty string, but only at the start or end of a word (a maximal sequence of alphanumeric characters; see also \w)
\b
matches an empty string, but not at the start or end of a word
\d
matches one digit, like the set [0-9]
\d
matches one non-digit, like the set [^0-9]
\s
matches a whitespace character, like the set [ \t\n\r\f\v]
\s
matches a non-white character, like the set [^ \t\n\r\f\v]
\w
matches one alphanumeric character; unless locale or unicode is set, \w is like [a-za-z0-9_]
\w
matches one non-alphanumeric character, the reverse of \w
\z
matches an empty string, but only at the end of the whole string
\\
matches one backslash character
9.7.2 common regular expression idioms
'.*' as a substring of a regular expression's pattern string means "any number of repetitions (zero or more) of any character." in other words, '.*' matches any substring of a target string, including the empty substring. '.+' is similar, but it matches only a non-empty substring. for example:
'pre.*post'
matches a string containing a substring 'pre' followed by a later substring 'post', even if the latter is adjacent to the former (e.g., it matches both 'prepost' and 'pre23post'). on the other hand:
'pre.+post'
matches only if 'pre' and 'post' are not adjacent (e.g., it matches 'pre23post' but does not match 'prepost'). both patterns also match strings that continue after the 'post'.
to constrain a pattern to match only strings that end with 'post', end the pattern with \z. for example:
r'pre.*post\z'
matches 'prepost', but not 'preposterous'. note that we need to express the pattern with raw-string syntax (or escape the backslash \ by doubling it into \\), as it contains a backslash. using raw-string syntax for all regular expression pattern literals is good practice in python, as it's the simplest way to ensure you'll never fail to escape a backslash.
another frequently used element in regular expression patterns is \b, which matches a word boundary. if you want to match the word 'his' only as a whole word and not its occurrences as a substring in such words as 'this' and 'history', the regular expression pattern is:
 r'\bhis\b'
with word boundaries both before and after. to match the beginning of any word starting with 'her', such as 'her' itself but also 'hermetic', but not words that just contain 'her' elsewhere, such as 'ether', use:
r'\bher'
with a word boundary before, but not after, the relevant string. to match the end of any word ending with 'its', such as 'its' itself but also 'fits', but not words that contain 'its' elsewhere, such as 'itsy', use:
r'its\b'
with a word boundary after, but not before, the relevant string. to match whole words thus constrained, rather than just their beginning or end, add a pattern element \w* to match zero or more word characters. for example, to match any full word starting with 'her', use:
r'\bher\w*'
and to match any full word ending with 'its', use:
r'\w*its\b'
9.7.3 sets of characters
you denote sets of characters in a pattern by listing the characters within brackets ([ ]). in addition to listing single characters, you can denote a range by giving the first and last characters of the range separated by a hyphen (-). the last character of the range is included in the set, which is different from other python ranges. within a set, special characters stand for themselves, except \, ], and -, which you must escape (by preceding them with a backslash) when their position is such that, unescaped, they would form part of the set's syntax. in a set, you can also denote a class of characters by escaped-letter notation, such as \d or \s. however, \b in a set denotes a backspace character, not a word boundary. if the first character in the set's pattern, right after the [, is a caret (^), the set is complemented. in other words, the set matches any character except those that follow ^ in the set pattern notation.
a frequent use of character sets is to match a word, using a definition of what characters can make up a word that differs from \w's default (letters and digits). to match a word of one or more characters, each of which can be a letter, an apostrophe, or a hyphen, but not a digit (e.g., 'finnegan-o'hara'), use:
r"[a-za-z'\-]+"
it's not strictly necessary to escape the hyphen with a backslash in this case, since its position makes it syntactically unambiguous. however, the backslash makes the pattern somewhat more readable, by visually distinguishing the hyphen that you want to have as a character in the set from those used to denote ranges.
9.7.4 alternatives
a vertical bar (|) in a regular expression pattern, used to specify alternatives, has low precedence. unless parentheses change the grouping, | applies to the whole pattern on either side, up to the start or end of the string, or to another |. a pattern can be made up of any number of subpatterns joined by |. to match such a regular expression, the first subpattern is tried first, and if it matches, the others are skipped. if the first subpattern does not match, the second subpattern is tried, and so on. | is neither greedy nor non-greedy, as it doesn't take into consideration the length of the match.
if you have a list l of words, a regular expression pattern that matches any of the words is:
'|'.join([r'\b%s\b' % word for word in l])
if the items of l can be more-general strings, not just words, you need to escape each of them with function re.escape, covered later in this chapter, and you probably don't want the \b word boundary markers on either side. in this case, use the regular expression pattern:
'|'.join(map(re.escape,l))
9.7.5 groups
a regular expression can contain any number of groups, from none up to 99 (any number is allowed, but only the first 99 groups are fully supported). parentheses in a pattern string indicate a group. element (?p<id>...) also indicates a group, and in addition gives the group a name, id, that can be any python identifier. all groups, named and unnamed, are numbered from left to right, 1 to 99, with group number 0 indicating the whole regular expression.
for any match of the regular expression with a string, each group matches a substring (possibly an empty one). when the regular expression uses |, some of the groups may not match any substring, although the regular expression as a whole does match the string. when a group doesn't match any substring, we say that the group does not participate in the match. an empty string '' is used to represent the matching substring for a group that does not participate in a match, except where otherwise indicated later in this chapter.
for example:
r'(.+)\1+\z'
matches a string made up of two or more repetitions of any non-empty substring. the (.+) part of the pattern matches any non-empty substring (any character, one or more times), and defines a group thanks to the parentheses. the \1+ part of the pattern matches one or more repetitions of the group, and the \z anchors the match to end-of-string.
9.7.6 optional flags
a regular expression pattern element with one or more of the letters "ilmsux" between (? and ) lets you set regular expression options within the regular expression's pattern, rather than by the flags argument to function compile of module re. options apply to the whole regular expression, no matter where the options element occurs in the pattern. for clarity, options should always be at the start of the pattern. placement at the start is mandatory if x is among the options, since x changes the way python parses the pattern.
using the explicit flags argument is more readable than placing an options element within the pattern. the flags argument to function compile is a coded integer, built by bitwise oring (with python's bitwise or operator, |) one or more of the following attributes of module re. each attribute has both a short name (one uppercase letter), for convenience, and a long name (an uppercase multiletter identifier), which is more readable and thus normally preferable:
i or ignorecase
makes matching case-insensitive
l or locale
causes \w, \w, \b, and \b matches to depend on what the current locale deems alphanumeric
m or multiline
makes the special characters ^ and $ match at the start and end of each line (i.e., right after/before a newline), as well as at the start and end of the whole string
s or dotall
causes the special character . to match any character, including a newline
u or unicode
makes \w, \w, \b, and \b matches depend on what unicode deems alphanumeric
x or verbose
causes whitespace in the pattern to be ignored, except when escaped or in a character set, and makes a # character in the pattern begin a comment that lasts until the end of the line
for example, here are three ways to define equivalent regular expressions with function compile, covered later in this chapter. each of these regular expressions matches the word "hello" in any mix of upper- and lowercase letters:
import re
r1 = re.compile(r'(?i)hello')
r2 = re.compile(r'hello', re.i)
r3 = re.compile(r'hello', re.ignorecase)
the third approach is clearly the most readable, and thus the most maintainable, even though it is slightly more verbose. note that the raw-string form is not necessary here, since the patterns do not include backslashes. however, using raw strings is still innocuous, and is the recommended style for clarity.
option re.verbose (or re.x) lets you make patterns more readable and understandable by appropriate use of whitespace and comments. complicated and verbose regular expression patterns are generally best represented by strings that take up more than one line, and therefore you normally want to use the triple-quoted raw-string format for such pattern strings. for example:
repat_num1 = r'(0[0-7]*|0x[\da-fa-f]+|[1-9]\d*)l?\z'
repat_num2 = r'''(?x)            # pattern matching integer numbers
              (0 [0-7]*        | # octal: leading 0, then 0+ octal digits
               0x [\da-f-a-f]+ | # hex: 0x, then 1+ hex digits
               [1-9] \d*       ) # decimal: leading non-0, then 0+ digits
               l?\z              # optional trailing l, then end of string
              '''
the two patterns defined in this example are equivalent, but the second one is made somewhat more readable by the comments and the free use of whitespace to group portions of the pattern in logical ways.
9.7.7 match versus search
so far, we've been using regular expressions to match strings. for example, the regular expression with pattern r'box' matches strings such as 'box' and 'boxes', but not 'inbox'. in other words, a regular expression match can be considered as implicitly anchored at the start of the target string, as if the regular expression's pattern started with \a.
often, you're interested in locating possible matches for a regular expression anywhere in the string, without any anchoring (e.g., find the r'box' match inside such strings as 'inbox', as well as in 'box' and 'boxes'). in this case, the python term for the operation is a search, as opposed to a match. for such searches, you use the search method of a regular expression object, while the match method only deals with matching from the start. for example:
import re
r1 = re.compile(r'box')
if r1.match('inbox'): print 'match succeeds'
else print 'match fails'                          # prints: match fails
if r1. search('inbox'): print 'search succeeds'   # prints: search succeeds
else print 'search fails'
9.7.8 anchoring at string start and end
the pattern elements ensuring that a regular expression search (or match) is anchored at string start and string end are \a and \z respectively. more traditionally, elements ^ for start and $ for end are also used in similar roles. ^ is the same as \a, and $ is the same as \z, for regular expression objects that are not multiline (i.e., that do not contain pattern element (?m) and are not compiled with the flag re.m or re.multiline). for a multiline regular expression object, however, ^ anchors at the start of any line (i.e., either at the start of the whole string or at the position right after a newline character \n). similarly, with a multiline regular expression, $ anchors at the end of any line (i.e., either at the end of the whole string or at the position right before \n). on the other hand, \a and \z anchor at the start and end of the whole string whether the regular expression object is multiline or not. for example, here's how to check if a file has any lines that end with digits:
import re
digatend = re.compile(r'\d$', re.multiline)
if re.search(open('afile.txt').read(  )): print "some lines end with digits"
else: print "no lines end with digits"
a pattern of r'\d\n' would be almost equivalent, but in that case the search would fail if the very last character of the file were a digit not followed by a terminating end-of-line character. with the example above, the search succeeds if a digit is at the very end of the file's contents, as well as in the more usual case where a digit is followed by an end-of-line character.
9.7.9 regular expression objects
a regular expression object r has the following read-only attributes detailing how r was built (by function compile of module re, covered later in this chapter):
flags
the flags argument passed to compile, or 0 when flags is omitted
groupindex
a dictionary whose keys are group names as defined by elements (?p<id>); the corresponding values are the named groups' numbers
pattern
the pattern string from which r is compiled
these attributes make it easy to get back from a compiled regular expression object to its pattern string and flags, so you never have to store those separately.
a regular expression object r also supplies methods to locate matches for r's regular expression within a string, as well as to perform substitutions on such matches. matches are generally represented by special objects, covered in the later section 9.7.10.
findall 
r.findall(s)
 
when r has no groups, findall returns a list of strings, each a substring of s that is a non-overlapping match with r. for example, here's how to print out all words in a file, one per line:
import re
reword = re.compile(r'\w+')
for aword in reword.findall(open('afile.txt').read(  )):
    print aword
when r has one group, findall also returns a list of strings, but each is the substring of s matching r's group. for example, if you want to print only words that are followed by whitespace (not punctuation), you need to change only one statement in the previous example:
reword = re.compile('(\w+)\s')
when r has n groups (where n is greater than 1), findall returns a list of tuples, one per non-overlapping match with r. each tuple has n items, one per group of r, the substring of s matching the group. for example, here's how to print the first and last word of each line that has at least two words:
import re
first_last = re.compile(r'^\w*(\w+)\b.*\b(\w+)\w*$',
                                      re.multiline)
for first, last in \
first_last.findall(open('afile.txt').read(  )):
    print first, last
match 
r.match(s,start=0,end=sys.maxint)
 
returns an appropriate match object when a substring of s, starting at index start and not reaching as far as index end, matches r. otherwise, match returns none. note that match is implicitly anchored at the starting position start in s. to search for a match with r through s, from start onwards, call r.search, not r.match. for example, here's how to print all lines in a file that start with digits:
import re
digs = re.compile(r'\d+')
for line in open('afile.txt'):
    if digs.match(line): print line,
search 
r.search(s,start=0,end=sys.maxint)
 
returns an appropriate match object for the leftmost substring of s, starting not before index start and not reaching as far as index end, that matches r. when no such substring exists, search returns none. for example, to print all lines containing digits, one simple approach is as follows:
import re
digs = re.compile(r'\d+')
for line in open('afile.txt'):
    if digs.search(line): print line,
split 
r.split(s,maxsplit=0)
 
returns a list l of the splits of s by r (i.e., the substrings of s that are separated by non-overlapping, non-empty matches with r). for example, to eliminate all occurrences of substring 'hello' from a string, in any mix of lowercase and uppercase letters, one way is:
import re
rehello = re.compile(r'hello', re.ignorecase)
astring = ''.join(rehello.split(astring))
when r has n groups, n more items are interleaved in l between each pair of splits. each of the n extra items is the substring of s matching r's corresponding group in that match, or none if that group did not participate in the match. for example, here's one way to remove whitespace only when it occurs between a colon and a digit:
import re
re_col_ws_dig = re.compile(r'(:)\s+(\d)')
astring = ''.join(re_col_ws_dig.split(astring))
if maxsplit is greater than 0, at most maxsplit splits are in l, each followed by n items as above, while the trailing substring of s after maxsplit matches of r, if any, is l's last item. for example, to remove only the first occurrence of substring 'hello' rather than all of them, change the last statement in the first example above to:
astring = ''.join(rehello.split(astring, 1))
sub 
r.sub(repl,s,count=0)
 
returns a copy of s where non-overlapping matches with r are replaced by repl, which can be either a string or a callable object, such as a function. an empty match is replaced only when not adjacent to the previous match. when count is greater than 0, only the first count matches of r within s are replaced. when count equals 0, all matches of r within s are replaced. for example, here's another way to remove only the first occurrence of substring 'hello' in any mix of cases:
import re
rehello = re.compile(r'hello', re.ignorecase)
astring = rehello.sub('', astring, 1)
without the final 1 argument to method sub, this example would remove all occurrences of 'hello'.
when repl is a callable object, repl must accept a single argument (a match object) and return a string to use as the replacement for the match. in this case, sub calls repl, with a suitable match-object argument, for each match with r that sub is replacing. for example, to uppercase all occurrences of words starting with 'h' and ending with 'o' in any mix of cases, you can use the following:
import re
h_word = re.compile(r'\bh\w+o\b', re.ignorecase)
def up(mo): return mo.group(0).upper(  )
astring = h_word.sub(up, astring)
method sub is a good way to get a callback to a callable you supply for every non-overlapping match of r in s, without an explicit loop, even when you don't need to perform any substitution. the following example shows this by using the sub method to build a function that works just like method findall for a regular expression without groups:
import re
def findall(r, s):
    result = [  ]
    def foundone(mo): result.append(mo.group(  ))
    r.sub(foundone, s)
    return result
the example needs python 2.2, not just because it uses lexically nested scopes, but because in python 2.2 re tolerates repl returning none and treats it as if it returned '', while in python 2.1 re was more pedantic and insisted on repl returning a string.
when repl is a string, sub uses repl itself as the replacement, except that it expands back references. a back reference is a substring of repl of the form \g<id>, where id is the name of a group in r (as established by syntax (?p<id>) in r's pattern string), or \dd, where dd is one or two digits, taken as a group number. each back reference, whether named or numbered, is replaced with the substring of s matching the group of r that the back reference indicates. for example, here's how to enclose every word in braces:
import re
grouped_word = re.compile('(\w+)')
astring = grouped_word.sub(r'{\1}', astring)
subn 
r.subn(repl,s,count=0)
 
subn is the same as sub, except that subn returns a pair (new_string, n) where n is the number of substitutions that subn has performed. for example, to count the number of occurrences of substring 'hello' in any mix of cases, one way is:
import re
rehello = re.compile(r'hello', re.ignorecase)
junk, count = rehello.subn('', astring)
print 'found', count, 'occurrences of "hello"'
9.7.10 match objects
match objects are created and returned by methods match and search of a regular expression object. there are also implicitly created by methods sub and subn when argument repl is callable, since in that case a suitable match object is passed as the actual argument on each call to repl. a match object m supplies the following attributes detailing how m was created:
pos
the start argument that was passed to search or match (i.e., the index into s where the search for a match began)
endpos
the end argument that was passed to search or match (i.e., the index into s before which the matching substring of s had to end)
lastgroup
the name of the last-matched group (none if the last-matched group has no name, or if no group participated in the match)
lastindex
the integer index (1 and up) of the last-matched group (none if no group participated in the match)
re
the regular expression object r whose method created m
string
the string s passed to match, search, sub, or subn
a match object m also supplies several methods.
end, span, start 
m.end(groupid=0)
m.span(groupid=0)
m.start(groupid=0)
 
these methods return the delimiting indices, within m.string, of the substring matching the group identified by groupid, where groupid can be a group number or name. when the matching substring is m.string[i:j], m.start returns i, m.end returns j, and m.span returns (i, j). when the group did not participate in the match, i and j are -1.
expand 
m.expand(s)
 
returns a copy of s where escape sequences and back references are replaced in the same way as for method r.sub, covered in the previous section.
group 
m.group(groupid=0,*groupids)
 
when called with a single argument groupid (a group number or name), group returns the substring matching the group identified by groupid, or none if that group did not participate in the match. the common idiom m.group( ), also spelled m.group(0), returns the whole matched substring, since group number 0 implicitly means the whole regular expression.
when group is called with multiple arguments, each argument must be a group number or name. group then returns a tuple with one item per argument, the substring matching the corresponding group, or none if that group did not participate in the match.
groups 
m.groups(default=none)
 
returns a tuple with one item per group in r. each item is the substring matching the corresponding group, or default if that group did not participate in the match.
groupdict 
m.groupdict(default=none)
 
returns a dictionary whose keys are the names of all named groups in r. the value for each name is the substring matching the corresponding group, or default if that group did not participate in the match.
9.7.11 functions of module re
the re module supplies the attributes listed in the earlier section section 9.7.6. it also provides a function that corresponds to each method of a regular expression object (findall, match, search, split, sub, and subn), each with an additional first argument, a pattern string that the function implicitly compiles into a regular expression object. it's generally preferable to compile pattern strings into regular expression objects explicitly and call the regular expression object's methods, but sometimes, for a one-off use of a regular expression pattern, calling functions of module re can be slightly handier. for example, to count the number of occurrences of substring 'hello' in any mix of cases, one function-based way is:
import re
junk, count = re.subn(r'(?i)hello', '', astring)
print 'found', count, 'occurrences of "hello"'
in cases such as this one, regular expression options (here, for example, case insensitivity) must be encoded as regular expression pattern elements (here, (?i)), since the functions of module re do not accept a flags argument.
module re also supplies error, the class of exceptions raised upon errors (generally, errors in the syntax of a pattern string), and two additional functions.
compile 
compile(pattern,flags=0)
 
creates and returns a regular expression object, parsing string pattern as per the syntax covered in section 9.7.1, and using integer flags as in the section section 9.7.6, both earlier in this chapter.
escape 
escape(s)
 
returns a copy of string s where each non-alphanumeric character is escaped (i.e., preceded by a backslash \). this is handy when you need to match string s literally as part (or all) of a regular expression pattern string.
[ team lib ]