-1

Why do the following two python split function calls generate difference result? (From here: https://docs.python.org/3/library/re.html#contents-of-module-re)

re.split(r'\W+', 'Words, words, words.')
['Words', 'words', 'words', '']
re.split(r'(\W+)', 'Words, words, words.')
['Words', ', ', 'words', ', ', 'words', '.', '']
Barmar
  • 669,327
  • 51
  • 454
  • 560
David
  • 19
  • 1
  • 1
  • 2
    When you put a capture group around the regexp, the delimiters that match it are included in the results. – Barmar May 20 '22 at 16:22
  • 1
    Didn't you see that explained in the documentation you linked to? **If capturing parentheses are used in pattern, then the text of all groups in the pattern are also returned as part of the resulting list.** – Barmar May 20 '22 at 16:24

0 Answers0