Python development application-Regular expression sorting search

Source: Internet
Author: User

The RE module provides 3 methods for the exact query of the input string, and match and search only return a substring of the matching condition, which can be understood as non-greedy mode, and FindAll will return a substring of n matching criteria, which can be understood as greedy mode

Re.match ()

Re.search ()

Re.findall ()

The #match () method works by matching the pattern at the beginning of the searched string so that it can find the matching object, and the match returns the object, which contains a lot of information

Match=re.match (R ' Dog ', ' dog Cat dog ') #只要匹配到满足条件的就不匹配了print match.group (0)  #dogprint match.start ()    #0print Match.end ()     #3match =re.match (R ' Cat ', ' dog cat dog ') print type (match) #<type ' Nonetype ' >  # Because Cat does not start at the beginning of the string, there is no match to

The #search () method is similar to match (), but the search () method does not restrict us from finding matches only from the beginning of the string, matching substrings until the match is reached or until the end of the string

Match=re.search (R ' Cat ', ' dog cat dog ') print match.group (0)   #cat, if not grouped, the default is the No. 0 set of print Match.start ()    #4print Match.end ()     #7

#findall返回的是列表

Match=re.findall (R ' Dog ', ' dog Cat Dog ') #匹配是整个字符串, each substring to match, matched to the string culling, followed by the string to continue to match the regular condition until the end of the string, how many matches how many print match #[' Dog ', ' dog ']

#使用 Mathch.group grouping uses (), grouping and non-grouping matching "Large substrings" are the same, but after grouping, these subgroups can be processed separately.

ContactInfo = ' Doe, john:555-1212 ' Match=re.search (R ' \w+, \w+: \s+ ', contactinfo) print match.group (0)    #Doe, John: 555-1212match = Re.search (R ' (\w+), (\w+): (\s+) ', ContactInfo) print match.group (0)   #Doe, john:555-1212, group No. 0 represents the matching " Large substring "to meet all conditions print Match.group (1)    #Doeprint match.group (2)     #Johnprint Match.group (3)     #555-1212

#当一个正则表达式有很多分组的时候, positioning in the order of group appearance will become unrealistic. Python also allows you to specify a group name by using the following statement:

Match = Re.search (R ' (? p<last>\w+), (? p<first>\w+): (? p<phone>\s+) ', ContactInfo) print Match.group (' last ')  #Doeprint match.group (' first ') #Johnprint Match.group (' phone ') #555-1212

The #尽管findall () method does not return a grouped object, and it can also use grouping. Similarly, the FindAll () method returns a collection of tuples, where the nth element in each tuple corresponds to the nth grouping in the regular expression.

Match=re.findall (R ' \w+, \w+: \s+ ', contactinfo) print match    #[' Doe, john:555-1212 ']

  

Match=re.findall (R ' (\w+), (\w+): (\s+) ', ContactInfo) print match   #[(' Doe ', ' John ', ' 555-1212 ')]

  

Python development application-Regular expression sorting search

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.