The match, search, FindAll, finditer differences of the RE in Python

Source: Internet
Author: User
Tags stdin

Original address: http://blog.csdn.net/djskl/article/details/44357389

These four methods are a common way to look for a particular substring from a string or to determine whether a string conforms to a pattern.

1, Match re.match (pattern, string[, flags]) starts with the first letter, and if string contains the pattern substring, the match succeeds, returns the Match object, fails returns none, and if the pattern is End With $.2, search re.search (pattern, string[, flags]) if the string contains the pattern substring, the match object is returned, otherwise none is returned, note that if there are multiple pattern substrings in the string, only the first One.3, FindAll Re.findall (pattern, string[, flags]) returns all strings in string that match the pattern, returned as an array.4, Finditer re.finditer (pattern, string[, flags]) returns all strings in string that match the pattern, returning as iterators. If the match succeeds, match ()/Search () returns the Match object, and Finditer () returns an iterator to the match object, which gets the group (), groups, or group (index) methods of the match object to be called. The difference between group (), groups () and group (index) is as follows:>>> ImportRe>>>S= ' 23432werwre2342werwrew '  >>>P= R ' (\d*) ([a-za-z]*) '  >>>M=Re.match (P,s)>>>M.group ()' 23432werwre '  >>>M.group (0)' 23432werwre '  >>>M.group (1)' 23432 '  >>>M.group (2)' Werwre '  >>>M.groups () (' 23432 ',' Werwre ')>>>M=Re.findall (P,s)>>>M [(' 23432 ',' Werwre '), (' 2342 ',' Werwrew '), ("',"')]>>>P=R ' (\d+) '  >>>M=Re.match (P,s)>>>M.group ()' 23432 '  >>>M.group (0)' 23432 '  >>>M.group (1)' 23432 '  >>>M.groups () (' 23432 ',)>>>M=Re.findall (P,s)>>>m [' 23432 ',' 2342 ': Group (): A substring in the string that matches the pattern patterns;0): The result is the same as group (); groups (): A tuple of all groups, group (1) is a successful substring that matches the first group in Patttern, group (2) is the second, and so on, if the index is over the boundary, throw Indexerror;findall (): Return is the array of all groups, that is, the group composed of tuples of the array, the cluster in the string to form a tuple, the measure to form a tuple, These tuples together form a list, which is the return result of FindAll (). Also, if groups is a tuple with only one element, the return result of FindAll is the list of substrings, not the tuple's list. Example S="1113446777"Use regular expressions to divide s into1111,3, -,6,777>>> ImportRe>>>S=' 1113446777 '  >>>M=Re.findall (R ' (\d) \1* ', s)>>> Printm [' 1 ',' 3 ',' 4 ',' 6 ',' 7 ']>>>M=Re.search (R ' (\d) \* ', s)>>>M.group ()>>>M=Re.search (R ' (\d) \1* ', s)>>>M.group ()' 111 '  >>>M.groups () (' 1 ',)>>>M.group (0)' 111 '  >>>M.group (1)' 1 '  >>>M.group (2) Traceback (most recent): File"<stdin>", line1,inch <Module>  Indexerror: No such group>>>M=Re.finditer (R ' (\d) \1* ', s)>>>M.Next(). Group ()' 111 '  >>>M.Next(). Group ()' 3 '  >>>M.Next(). Group ()' a '  >>>M.Next(). Group ()' 6 '  >>>M.Next(). Group ()' 777 '  >>>M.Next(). Group () Traceback (most recent call last): File"<stdin>", line1,inch <Module>  stopiterationAnother example:>>>P= R ' (\d) \1+ ([a-za-z]+) '  >>>S= ' 1111werwrw3333rertert4444 '  >>>P= R ' (\d) \1+ ([a-za-z]*) '  >>> ImportRe>>>Re.findall (P,s) [(' 1 ',' WERWRW '), (' 3 ',' Rertert '), (' 4 ',"')]>>>M=Re.search (P,s)>>>M.group ()' 1111WERWRW '  >>>M.group (1)' 1 '  >>>M.group (2)' WERWRW '  >>>M.groups () (' 1 ',' WERWRW ')>>>M=Re.finditer (P,s)>>>M.Next(). Group ()' 1111WERWRW '  >>>M.Next(). Group ()' 3333rertert '  >>>M.Next(). Group ()' 4444 '  >>>M.Next(). Group () Traceback (most recent call last): File"<stdin>", line1,inch <Module>  stopiteration  

The match, search, FindAll, finditer differences of the RE in Python

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.