Python Regular Expressions (5)--findall, Finditer methods

Source: Internet
Author: User

FindAll method

Compared to other methods, the FindAll method is somewhat special. It does this by looking up all the strings in the string that can be matched, saving the results in the list, and then returning the list

Note: match and search are matched once findall match all.

1 Pattern.findall method

The function of this method is to find all the substrings that satisfy the pattern in the String[pos, Endpos] interval from the POS subscript, until the endpos position ends, and returns the result of the lookup as a list, and returns an empty list if it is not found.

Syntax format:

Pattern.findall(string[, pos[, Endpos]])

2 Re.findall

Gets all the strings in the string that can be matched and returns as a list.

Syntax format:

Re.findall(pattern, string, flags=0 )

3 when pattern has parentheses (grouping), the string in the list is only the contents of the parentheses, not the entire regular expression.(1) When a regular expression contains multiple parentheses ()

The elements in the returned list consist of all content that satisfies the match, but each element is a tuple of content that matches all the parentheses in the expression

>>> Re.findall (R ' A (b) (c) ', ' abcabc ')

[(' B ', ' C '), (' B ', ' C ')]

(2) When there is only one parenthesis in the regular expression

The elements of the returned list are surrounded by all the expressions that can be successfully matched

And the elements in the list are strings

>>> Re.findall (R ' A (b) C ', ' ABCABC ')

[' B ', ' B ']

(3) When there are no parentheses in the regular expression

The elements in the returned list are made up of all substrings that can successfully match.

>>> Re.findall (R ' ABC ', ' Abcabc ')

[' ABC ', ' ABC ']

Finditer method

The Finditer function is similar to the FindAll function, but returns an iterator instead of a list of all results, like the FindAll function.

Each object of Finditer can use group (which can get the entire matching string) and groups method;

In the case of grouping, FindAll can only get the grouping and cannot get the whole matching string.

>>> Re.findall (R ' A (b) (c) ', ' ABCD 12abcde ')

[(' B ', ' C '), (' B ', ' C ')]

>>> a = Re.finditer (R ' A (b) (c) ', ' ABCD 12abcde ')

>>> for I in a:

... print i.group ()

...

Abc

Abc

>>> a = Re.finditer (R ' A (b) (c) ', ' ABCD 12abcde ')

>>> for I in a:

... print i.groups ()

...

(' B ', ' C ')

(' B ', ' C ')

Python Regular Expressions (5)--findall, Finditer methods

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.