Pro!

Eff-bot trick of an "alternating" regex, for use with "findall", used this time for whitespace vs. non-whitespace

_re_whitespace_alternate = re.compile(r'[ \t\r\n]+|[^ \t\r\n]+')

__test__['_re_whitespace_alternate'] = r"""

>>> def f(s):
... return _re_whitespace_alternate.findall(s)

>>> f(' hello ')
[' ', 'hello', ' ']
>>> f(' \t hello hello \t ')
[' \t ', 'hello', ' ', 'hello', ' \t ']

"""

Forward

2 months, 1 week ago by Manuel "Moe" G. - Pro!

Add a Reply

Join Now to Reply!

To add your own reply, sign in or join Pownce today!

This note is also available to the general public.