Parentheses matching problem in Python regular expressions

Source: Internet
Author: User
Problem:

m = Re.findall (' [0-9]*4[0-9]* ', ' [4] ')
can match to 4.
m = Re.findall (' ([0-9]) ([0-9]) * ', ' [4] ')
Matches less than 4.
What is this for? PS, this is a simplified description, I want to use a more complex than this, so to use (), to indicate a sequence of matching.
To add, when I put it in notepad++, both types of writing can be matched, and I don't know why Python is out of the question.

Answer:

The Python's regular () matches, so the return result is [', '], which is the match in two (). To achieve the original matching effect, is to match 4 out, there are two ways to solve:

1. Add a brace to the outermost layer to: M = Re.findall (' ([[0-9]) ([0-9]) ', ' [4] '), the first element of the returned result is the matching result.
2. Remove () The match result returned, in front of parentheses to add?:, Into M = Re.findall (?: \ d) (?: \ d) * ', ' [4] ', return result is to match the result.

  • 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.