Does * have a side effect in matching Python regex?

I am learning Python regex, the following works as I expected:

>>> import re
>>> re.split('\s+|:', 'find   a str:s2')
['find', 'a', 'str', 's2']

But when I change +to *, the conclusion is strange to me:

>>> re.split('\s*|:', 'find  a str:s2')
['find', 'a', 'str:s2']

How is such a pattern interpreted in Python?

+4
source share
2 answers

The “side effect” that you see is that it re.split()will only be divided into matches that are longer than 0 characters.

\s*|: , , :, , . . , , .

\s* , , : .

re.split():

, .

, :, :

>>> re.split(':|\s*', 'find  a str:s2')
['find', 'a', 'str', 's2']
+8

"" , - : re.split('(\s*|:)', 'find a str:s2') : "+" " ". "*" any ( none)

-4

Source: https://habr.com/ru/post/1545857/


All Articles