1.re.search (): Search returns an object that finds results (after finding the result of the first successful match in order, not looking back, no finding returns None), you can use the group () or groups () method to get a string that matches successfully.
①group () returns the entire string with a successful match (ignoring the parentheses in the pattern), or a string (counting from 1) that returns the number of parentheses in the matching success.
②groups () returns the contents of the parentheses in the matching pattern in the form of a tuple, and returns the empty tuple for the string that successfully matches if there are no parentheses in the pattern.
1 >>> string = ' python ' 2 >>> import re 3 >>> result = Re.search (R ' (YT) H (O) ', string) 4 >> ;> result 5 <_sre. Sre_match object at 0x000000000293de88> 6 >>> result.group () 7 ' Ytho ' 8 >>> result.group (0) # parameter 0 Invalid 9 ' Ytho ' >>> result.group (1) # starting from 1 count one ' YT ' >>> result.group (2) ' O ' + >>> result . Groups (' YT ', ' o ') >>> result.groups (0) # Invalid incoming parameter (' YT ', ' o ') >>> result.groups (1) 19 (' YT ', ' o ') >>>
2. Re.finditer (): An iterator that returns all lookup results (returns an empty iterator if there is no successful string), and each iteration object can also use Group () and groups () to obtain the result of a successful match.
1 >>> string = ' One11python, two22, Three33python ' 2 >>> result = Re.finditer (R ' (\d+) (Python) ', string) 3 >>> for P in result:4 print (P.group ()) 5 6 7 11python 8 33python 9 >>> for P in result:1 0 Print (P.group (2)) python14 python15 >>> for P in result:16 print (p.groups ()) # If no parentheses are in the pattern, the empty tuple corresponding to each iterator is returned. (' One ', ' Python ') (' + ', ' python ')
3. Re.findall (): Returns all found strings as a list (returns an empty list if no string is found to match successfully).
1 >>> string = ' One11python, two22, Three33python ' 2 >>> result = Re.findall (R ' \d+python ', string) 3 >>> result4 [' 11python ', ' 33python ']5 >>> result = Re.findall (R ' (\d +) (Python) ', string ' 6 >>> result7 [(' One ', ' Python '), (' + ', ' python ')]