原文来源:https://stackoverflow.com/questions/2136556/in-python-how-do-i-split-a-string-and-keep-the-separators

问:
下面是最简单的解释:我是这么用的

re.split('\W', 'foo/bar spam\neggs')
-> ['foo', 'bar', 'spam', 'eggs']

但是我想要的是下面这样的

someMethod('\W', 'foo/bar spam\neggs')
-> ['foo', '/', 'bar', ' ', 'spam', '\n', 'eggs']

因为我想将一个字符串拆分成标记,操纵它,然后再将它重新组合在一起。

答:

 >>> re.split('(\W)', 'foo/bar spam\neggs')
['foo', '/', 'bar', ' ', 'spam', '\n', 'eggs']

相关文章:

  • 2022-12-23
  • 2021-09-13
  • 2021-08-26
  • 2022-12-23
  • 2021-11-22
  • 2022-12-23
  • 2021-08-27
  • 2022-12-23
猜你喜欢
  • 2021-12-30
  • 2022-12-23
  • 2022-02-13
  • 2022-12-23
  • 2021-12-29
  • 2021-04-22
  • 2021-05-30
相关资源
相似解决方案