注:一个()代表一个分组
https://segmentfault.com/q/1010000000094918
http://www.tutorialspoint.com/python/python_reg_expressions.htm
http://www.cnblogs.com/huxi/archive/2010/07/04/1771073.html python正则表达式指南
The match Function
This function attempts to match RE pattern to string with optional flags.
Here is the syntax for this function −
re.match(pattern, string, flags=0)
Here is the description of the parameters:
| Parameter | Description |
|---|---|
| pattern | This is the regular expression to be matched. |
| string | This is the string, which would be searched to match the pattern at the beginning of string. |
| flags | You can specify different flags using bitwise OR (|). These are modifiers, which are listed in the table below. |
The re.match function returns a match object on success, None on failure. We usegroup(num) or groups() function of match object to get matched expression.
| Match Object Methods | Description |
|---|---|
| group(num=0) | This method returns entire match (or specific subgroup num) |
| groups() | This method returns all matching subgroups in a tuple (empty if there weren't any) |
Example
#!/usr/bin/python import re line = "Cats are smarter than dogs" matchObj = re.match( r'(.*) are (.*?) .*', line, re.M|re.I) if matchObj: print "matchObj.group() : ", matchObj.group() print "matchObj.group(1) : ", matchObj.group(1) print "matchObj.group(2) : ", matchObj.group(2) else: print "No match!!"
When the above code is executed, it produces following result −
matchObj.group() : Cats are smarter than dogs matchObj.group(1) : Cats matchObj.group(2) : smarter
matchObj.groups()
('Cats', 'smarter')
浙公网安备 33010602011771号