Detailed python regular simple instance code

Source: Internet
Author: User
This article mainly introduces the Python regular simple example, specifically analyzes the python's simple regular matching test for strings and related considerations, the need for friends can refer to the following

The examples in this article describe Python's regular simple usage. Share to everyone for your reference, as follows:

Quietly into the company's internal ued of a small group of Python enthusiasts, two days ago a cow people sent a message:

Small test questions:

Re.split (' (\w+) ', ' Test, test, test. ')

What results are returned

At first, I did not notice that W is capitalized, and that the lowercase w represents the word character (with an underscore), which is only found to be capitalized if it is run today.

The results of the idle run are as follows:

>>> Import re>>> re.split (' (\w+) ', ' Test, test, test. ') [', ', ', ' test ', ', ', ' test ', ', ', ' test ', '. ', ']>>>

Seeing the output as above, I was puzzled, \w matches non-word characters, then why are there so many non-word characters in the result?

I doubt that my meaning to \w is mistaken, open the regular handbook and look, make sure I remember correctly, I found out that the matching pattern in this example contains parentheses, which correspond to the pattern in the regular

This means that the match is obtained at the same time and is saved to the matching result set.

Suddenly.

Re-test:

>>> re.split (' (\w+) ', ' Test, test, test. ') [', ', ', ' test ', ', ', ' test ', ', ', ', ', ', ', '. ', ']>>> re.split ' (' \w+ ', ' Test, test, test. ') [', ' Test ', ' test ', ' Test ', ']>>> '

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.