Efficiently split a string using multiple separators and retaining each separator?

前端 未结 9 1456
野趣味
野趣味 2021-02-02 10:44

I need to split strings of data using each character from string.punctuation and string.whitespace as a separator.

Furthermore, I need for the

9条回答
  •  情深已故
    2021-02-02 11:27

    Try this:

    import re
    re.split('(['+re.escape(string.punctuation + string.whitespace)+']+)',"Now is the winter of our discontent")
    

    Explanation from the Python documentation:

    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.

提交回复
热议问题