Python re module findall () function instance parsing, pythonfindall

Source: Internet
Author: User

Python re module findall () function instance parsing, pythonfindall

This article studies the findall () function of the re module. First, let's look at the instance code:

>>> import re >>> s = "adfad asdfasdf asdfas asdfawef asd adsfas "  >>> reObj1 = re.compile('((\w+)\s+\w+)') >>> reObj1.findall(s) [('adfad asdfasdf', 'adfad'), ('asdfas asdfawef', 'asdfas'), ('asd adsfas', 'asd')]  >>> reObj2 = re.compile('(\w+)\s+\w+') >>> reObj2.findall(s) ['adfad', 'asdfas', 'asd']  >>> reObj3 = re.compile('\w+\s+\w+') >>> reObj3.findall(s) ['adfad asdfasdf', 'asdfas asdfawef', 'asd adsfas'] 

Use the preceding code example to explain:

The findall function always returns a list Of all matching results of the Regular Expression in the string. Here we mainly discuss the display method of the "result" in the list, that is, the findall function returns the information contained by each element in the list.

@ 1. when the given regular expression contains multiple parentheses, the list elements are tuple consisting of multiple strings. The number of strings in the tuple is the same as the logarithm of the parentheses, the content of the string corresponds to the Regular Expression in each bracket, and the discharge order is the order in which the brackets appear.

@ 2. when the given regular expression contains a bracket, the list element is a string. The content of this string corresponds to the regular expression in the bracket (not the Matching content of the entire regular expression ).

@ 3. When the given regular expression does not contain parentheses, the list element is a string, which is the content that the entire regular expression matches.

Summary

The above is all the content about parsing the findall () function instance of the python re module. I hope it will be helpful to you. If you are interested, you can continue to refer to other related topics on this site. If you have any shortcomings, please leave a message. Thank you for your support!

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.