Python re.search and Re.match regular expressions
A Re.search and Re.match
Python provides the main regular expression operations in 2: Re.match and Re.search.
Match: Matches only from the beginning of the string with the regular expression, the match returns Matchobject successfully, otherwise none is returned;
Search: All string attempts to match the regular expression, if all the strings are not matched successfully, return none, otherwise return matchobject; (re.search equivalent to the default behavior in Perl)
Instance code:
Import re
Def testsearchandmatch ():
S1= "HelloWorld, I am 30!"
W1 = "World"
M1 = Re.search (W1, S1)
If M1:
Print ("Find:%s"% M1.group ())
If Re.match (w1, S1) = none:
Print ("Cannot match")
W2 = "HelloWorld"
M2 = Re.match (W2, S1)
If M2:
Print ("match:%s"% M2.group ())
Testsearchandmatch ()
#find: World
#cannot match
#match: HelloWorld
Two Re.compile and Re.ignorecase
Re.compile returns the Regrexobject object used to reuse the regrexobject;
Re.ignorecase is used to ignore case when matching;
Instance code:
Def testcompile ():
Regex = "D{3}-d{7}"
Regexobject = Re.compile (regex)
Print (Regexobject.search ("AAA 027-4567892"). Group ())
Print (Regexobject.search ("BBB 021-1234567"). Group ())
Print (Regexobject.search ("CCC 010-123456"))
Testcompile ()
#027-4567892
#021-1234567
#none
Def testignorecase ():
Print (Re.search, "Hello World!"). Group ())
Print (Re.search, "Hello World!", Re.ignorecase). Group ())
Print (Re.search, "Hello World!")
Testignorecase ()
#world
#world
#none
Three Matchobject
Matchobject is the object returned after a successful match for Re.search,re.match, and contains the result of the match.
In regular expressions, you can use () to group and number some regular expressions, numbering starting with 1, using numbers, such as 1 2 3, and (?p<name>) naming groups, using (?). P=name) to use a named group.
Matchobject.group () contains all the matching content, equivalent to Matchobject.group (0), at which point 0 indicates all matches;
The Matchobject.groups tutorial () contains the matching content for all the groups defined in the regular expression that use ();
Matchobject.group (n), which returns the content of the Nth group () match in the regular expression, where n is not 0, equivalent to matchobject.groups () [n-1];
Matchobject.lastindex, representing the number of groups () in the regular expression;
Instance code:
Def testmatchobject ():
m = Re.match (? P<year>d{4})-(? P<month>d{2})-(? P<date>d{2}) ", 2010-10-01, I am very happy")
Print (M.group ())
Print (M.group (0))
Print (M.groups ())
Print (M.group (1))
Print (m.groups () [0])
Print (M.group (2))
Print (M.groups () [1])
Print (M.group (3))
Print (M.groups () [2])
Print (M.groupdict ())
Print (M.lastindex)
Testmatchobject ()
#2010-10-01
#2010-10-01
# (' 2010 ', ' 10 ', ' 01 ')
#2010
#2010
#10
#10
#01
#01
#{' Date ': ' Year ', ' Month ': ' 10 '}
#3
Methods of four re and Matchobject Split+findall+finditer+sub
The split method, which uses the given expression to segment the string;
FindAll method that returns all a list that matches the given expression;
Finditer method that returns all the iterator of the matchobject that match the given expression;
Sub method, which replaces the string of the expression matching with the new string;