A
Arved Sandstrom
A generator, IOW.[ SNIP ]On 3/27/12 2:21 PM, Arne Vajhøj wrote:
On 3/27/2012 12:14 AM, Daniel Pitts wrote:
On 3/26/12 6:58 PM, Arne Vajhøj wrote:
On 3/26/2012 2:54 PM, laredotornado wrote:
I'm using Java 6. I want to split a Java string on a regular
expression, but I would like to keep part of the string used to
split
in the results. What I have are Strings like
Fri 7:30 PM, Sat 2 PM, Sun 2:30 PM
What I would like to do is split the expression wherever I have an
expression matching /(am|pm),?/i . Hopefully I got that right. In
the above example, I would like the results to be
Fri 7:30 PM
Sat 2 PM
Sun 2:30 PM
But with String.split, the split token is not kept within the
results. How would I write a Java parsing expression to do what I
want?
A hackish solution:
String[] p = s.replaceAll("[AP]M", "$0X$0").split("X[AP]M");
Nice. As far as hackish, using "split" for this purpose at all is
hackish.
That type of split is the typical way in most modern languages
(though usually in a non regex flavor).
For functional languages, yes, but those modern languages don't
necessarily return an array. Ideally they would return "iterable" of
some sort.
These days what's the difference? Both arrays and lists, in computing,
are commonly considered to support indexing, and both can be "iterated"
over one way or the other. As far as arrays go, consider what you can do
with Haskell arrays, or with array operations in APL or J, or with
slices in D...no "for" loops happening there.
I think what Daniel wanted was a lazy not an eager split.
Instead of doing a full parse and return a data structure
(array or list) then just return an iterator with a pointer
to the start and then do the parsing when asked for next.
Arne
AHS