Expression

Regular expressions are a special character sequence, it can help you to easily check whether a string matches a pattern.

Since Python 1.5 version adds the re module that provides Perl-style regular expression pattern.

re the Python language module has all the features of regular expressions.

compile function generates a regular expression object according to a pattern string flag and an optional parameter. This object has a series of methods for regular expression matching and replacement.

re module provides functions identical with the functions of these methods, which use a pattern string functions as their first argument.

This section introduces the Python commonly used regular expression handler.

re.match function
re.match try to match a pattern from a starting position of the string, the start position if not successful match, match () returns none.

Function Syntax:
re.match (pattern, String, the flags = 0)

Function Parameters:

Parameter Description
pattern matching a regular expression
string to match a string of
flags flag bits for controlling the regular expression matching method, such as: whether or not case-sensitive, multi-line matching and the like. See also: Regular expression modifier - optional flag
to match the success of re.match method returns an object matching, otherwise None.

We can use the group (num) or groups () function to obtain the matching object matching expression.

The method of matching objects described
string entire expression group (num = 0) match, Group () may be a plurality of input group number, in which case it will return those containing a group corresponding to the tuple values.
groups () Returns a string that contains the tuple of all groups, the group number included from 1 to.
Examples
 ! # / Usr / bin / Python
 # - Coding: UTF-. 8 -
Import Re
Print (. Re.match ( 'WWW', 'www.runoob.com') span ()) in the starting position # Match
print (re.match ( 'com', 'www.runoob.com')) # does not match the start position of
the above example the output operation is:
(0,. 3)
None

re.compile function
compile functions are used to compile a regular expression, generate a regular expression (the Pattern) object for match () and search () function uses these two.

The syntax is:

re.compile (pattern [, flags])
Parameters:

pattern: a string of a regular expression
flags: optional, it represents the matching pattern, such as ignoring the case, multi-line mode, for the specific parameters:
re.I ignore case
re.L represent special characters \ W, \ W, \ b, \ B, \ s, \ S depending on the current environmental
re.M multiline mode
re.S ie. including newline characters and optionally including (not included newline)
re.U indicate special charset \ w, \ W, \ b , \ B, \ d, \ D, \ s, \ S depends on the Unicode character properties database
re.X for readability and ignoring annotations whitespace # behind
regex modifier - optional flag
regular expressions can contain some optional flags modifiers to control the match mode. Modifier is specified as an optional sign. They specify | () Multiple flags can be bitwise OR through. The re.I | re.M I, and M is set to flag:

Modifier Description
re.I the match is not case sensitive
re.L do localization identification (locale-aware) matching
re.M multi-line matching, affecting ^ and $
re.S the Matches including all characters, including newline
re.U parse character based on Unicode character set. This flag affect \ w, \ W, \ b, \ B.
Re.X the mark by giving you more flexibility in format so that you will write regular expressions easier to understand.
--------------------- 

Guess you like

Origin www.cnblogs.com/ly570/p/10954615.html