Share a simple Python regular expression code example

Source: Internet
Author: User
This article mainly introduces the Python regular expression simple example, and analyzes the problems encountered in the Python simple string regular expression matching test and related precautions, for more information about Python regular expressions, see the next article, for more information, see

This example describes the simple usage of Python regular expressions. We will share this with you for your reference. The details are as follows:

A Python enthusiast who quietly entered the company's UED group sent a message two days ago:

Small test questions:

re.split('(\W+)', ' test, test, test.')

Returned results

At the beginning, I didn't notice that W is in upper case. I thought that w is in lower case and represents word characters (including underscores). I found it in upper case only when I ran it today.

Run the following command in IDLE:

>>> import re>>> re.split('(\W+)', ' test, test, test.')['', ' ', 'test', ', ', 'test', ', ', 'test', '.', '']>>>

I am confused when I see the above output. \ W matches non-word characters. why are there so many non-word characters in the result?

I have all doubts about the meaning of \ W. I opened the regular expression manual and checked it out. After I remembered it, I found that the matching pattern in this example contains parentheses, corresponds to the pattern in the regular expression ),

This means that the matching will be obtained at the same time and saved to the matching result set.

Suddenly.

Retest:

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

The above is to share the details of a simple Python regular code instance. For more information, see other related articles in the first PHP community!

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.