Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!news.musoftware.de!wum.musoftware.de!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail From: Robert Klemme Newsgroups: comp.lang.java.programmer Subject: Re: Keeping the split token in a Java regular expression Date: Mon, 26 Mar 2012 22:01:44 +0200 Lines: 28 Message-ID: <9tc099Fh7cU1@mid.individual.net> References: <48d35bc3-a391-4ccf-a222-dac64775a2f2@oq7g2000pbb.googlegroups.com> <33095746.178.1332789765559.JavaMail.geo-discussion-forums@pbcto7> Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Trace: individual.net MVzeGnukAV5scXUXq7j5nArpTzZXhKHltfHTrr98unKbl+bN4= Cancel-Lock: sha1:vIfFHFo9iArs9+TPUZEXYFNXF7o= User-Agent: Mozilla/5.0 (X11; Linux i686; rv:11.0) Gecko/20120310 Thunderbird/11.0 In-Reply-To: <33095746.178.1332789765559.JavaMail.geo-discussion-forums@pbcto7> Xref: csiph.com comp.lang.java.programmer:13196 On 03/26/2012 09:22 PM, Lew wrote: > 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? > > Based on what you've shown it looks like you could split on the comma and trim the resulting strings. And one wouldn't even need a regular expression for that. http://docs.oracle.com/javase/6/docs/api/java/util/StringTokenizer.html Kind regards robert