George Sakkis wrote:
> If you don't want any null strings at the beginning or the end, an
> equivalent regexp is:
>
whitespaceSplitter_2 = re.compile("\w+|\s+")
whitespaceSplitter_2.findall("1 2 3 \t\n5")
> ['1', ' ', '2', ' ', '3', ' \t\n', '5']
whitespaceSplitter_2.findall(
Jeremy Bowers wrote:
> On Fri, 01 Apr 2005 14:20:51 -0800, RickMuller wrote:
>
> > I'm trying to split a string into pieces on whitespace, but I want
to
> > save the whitespace characters rather than discarding them.
> >
> > For example, I want to split the string '12' into ['1','
','2'].
> > I
Thanks to everyone who responded!! I guess I have to study my regular
expressions a little more closely.
--
http://mail.python.org/mailman/listinfo/python-list
On Fri, 01 Apr 2005 18:01:49 -0500, Brian Beck wrote:
> py> from itertools import groupby
> py> [''.join(g) for k, g in groupby(' test ing ', lambda x: x.isspace())]
> [' ', 'test', ' ', 'ing', ' ']
>
> I tried replacing the lambda thing with an attrgetter, but apparently my
> understanding of
[Brian Beck]>
> py> from itertools import groupby
> py> [''.join(g) for k, g in groupby(' test ing ', lambda x: x.isspace())]
> [' ', 'test', ' ', 'ing', ' ']
Brilliant solution!
That leads to a better understanding of groupby as a tool for identifying
transitions without consuming them.
> I
RickMuller wrote:
There's a chance I was instead thinking of something in the re module,
but I also spent some time there without luck. Could someone point me
to the right function, if it exists?
The re solution Jeremy Bowers is what you want. Here's another (probably
much slower) way for fun (wit
On Fri, 01 Apr 2005 14:20:51 -0800, RickMuller wrote:
> I'm trying to split a string into pieces on whitespace, but I want to
> save the whitespace characters rather than discarding them.
>
> For example, I want to split the string '12' into ['1','','2'].
> I was certain that there was a
I'm trying to split a string into pieces on whitespace, but I want to
save the whitespace characters rather than discarding them.
For example, I want to split the string '12' into ['1','','2'].
I was certain that there was a way to do this using the standard string
functions, but I just sp