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!