且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

将字符串拆分为迭代器

更新时间:2022-06-14 21:20:45

不直接拆分字符串,但 re 模块有 re.finditer() (以及任何已编译的正则表达式上相应的 finditer()方法) 。

Not directly splitting strings as such, but the re module has re.finditer() (and corresponding finditer() method on any compiled regular expression).

@Zero要求举个例子:

@Zero asked for an example:

>>> import re
>>> s = "The quick    brown\nfox"
>>> for m in re.finditer('\S+', s):
...     print(m.span(), m.group(0))
... 
(0, 3) The
(4, 9) quick
(13, 18) brown
(19, 22) fox