Python Regular Expression learning and python Learning
Python group ()
In a regular expression, group () is used to propose a string intercepted by a group., () For grouping
Import rea = "123abc456" print re. search ("([0-9] *) ([a-z] *) ([0-9] *)", ). group (0) #123abc456, returns the overall print re. search ("([0-9] *) ([a-z] *) ([0-9] *)", ). group (1) #123 print re. search ("([0-9] *) ([a-z] *) ([0-9] *)", ). group (2) # abcprint re. search ("([0-9] *) ([a-z] *) ([0-9] *)", ). group (3) #456
Causes
1. In the regular expression, the matching results are divided into three groups by three sets of parentheses.
- Group () and group (0) are the results of matching the regular expression.
- Group (1) lists the Matching Parts of the first bracket, group (2) lists the Matching Parts of the second bracket, and group (3) lists the matching parts of the third bracket.
2. If no matching is successful, re. search () returns None.
3. Of course, the regular expression does not contain parentheses, and group (1) is definitely incorrect.
NOTE: For the first time, copy is only for self-study.
Link: http://www.cnblogs.com/kaituorensheng/archive/2012/08/20/2648209.html
The regular expression intercepts the URL File Name and suffix
Example:/upload/activity/xueba/20161025/091909 _ce15d6bf-1089-4002-9ff6-016e13e420e5.gif
1. Get the image name
Regular Expression: [^ \/\] + $
Matching result: 091909_ce15d6bf-1089-4002-9ff6-016e13e420e5.gif
2. Obtain the suffix.
Regular Expression: [^ \.] \ w * $
Matching result: gif
pic_type = re.search(r'[^\.]\w*$', pic_url).group(0)