Re module
The RE module enables the Python language to have all the regular expression functionality.
The compile function generates a regular expression object based on a pattern string and an optional flag parameter. The object has a series of methods for regular expression matching and substitution.
The RE module also provides functions that are fully consistent with these methods, which use a pattern string as their first parameter.
Re.match function
Re.match attempts to match a pattern from the starting position of the string, and if the match is not successful, match () returns none.
# (matching rules, strings, special flags)Re.match (pattern, string, flags=0)
Re.search method
Re.search scans the entire string and returns the first successful match.
Re.search (Pattern, string, flags=0)
The difference between Re.match and Re.search
Re.match matches only the beginning of the string, if the string does not begin to conform to the regular expression, the match fails, the function returns none, and Re.search matches the entire string until a match is found.
#!/usr/bin/pythonImportRe line="Cats is smarter than dogs"; Matchobj= Re.match (r'Dogs', line, re. m|Re. I)ifMatchobj:Print "match--matchobj.group ():", Matchobj.group ()Else: Print "No match!!"Matchobj= Re.search (r'Dogs', line, re. m|Re. I)ifMatchobj:Print "Search--Matchobj.group ():", Matchobj.group ()Else: Print "No match!!"
-Matchobj.group (): dogs
Retrieving and replacing
The Python re module provides re.sub to replace matches in a string.
# pattern string in the pattern regular. # repl the replacement string, or a function. # string to be found to replace the original string. # The maximum number of times to replace after the count pattern matches, and the default of 0 means to replace all matches. re.sub (Pattern, Repl, String, count=0, flags=0)
## # # instances#!/usr/bin/python#-*-coding:utf-8-*- ImportRe Phone="2004-959-559 # This is a foreign phone number" #Delete a python comment from a stringnum = Re.sub (r'#.*$',"", phone)Print "The phone number is:", Num#Delete A string that is not a number (-)num = Re.sub (r'\d',"", phone)Print "The phone number is:"Num
Phone number is: 2004-959-559 Phone number is:2004959559
Re.compile function
The compile function compiles a regular expression and generates a regular expression (Pattern) object for use by the match () and search () functions.
# # # # Parttern Re.compile (pattern[, flags]) # # re. I Ignore case # re. L represents the special character set \w, \w, \b, \b, \s, \s dependent on the current environment # re. M Multi-line mode # re. S is the. and includes a newline character (. Do not include line breaks)# re. U represents a special character set \w, \w, \b, \b, \d, \d, \s, \s dependent on the Unicode character attribute database # re. X in order to increase readability, ignore whitespace and comment after # re.compile (pattern[, flags])
>>>ImportRe>>> pattern = Re.compile (r'([a-z]+) ([a-z]+)', Re. I)#Re. I means ignoring case>>> m = Pattern.match ('Hello World Wide Web')>>>PrintM#match succeeds, returns a match object<_sre. Sre_match object at 0x10bea83e8>>>> m.group (0)#returns the entire substring of a successful match'Hello World'>>> M.span (0)#returns the index of the entire substring that matched the success(0, 11)>>> M.group (1)#returns the substring of the first group that matched a successful string'Hello'>>> M.span (1)#returns the index of the first subgroup that matched a successful substring(0, 5)>>> M.group (2)#returns the substring of the second group that matched a successful string' World'>>> M.span (2)#returns the substring of the second group that matched a successful string(6, 11)>>> m.groups ()#equivalent to (M.group (1), M.group (2), ...)('Hello',' World')>>> M.group (3)#there is no third groupingTraceback (most recent): File"<stdin>", Line 1,inch<module>Indexerror:no such group
Python-re Regular Matching module