Python Regular Expression learning and python Learning

Source: Internet
Author: User

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)

 


 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.