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 ', ']>>> '