![]() |
Keeping the split token in a Java regular expression
Hi,
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? Thanks, - Dave |
Re: Keeping the split token in a Java regular expression
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. -- Lew |
Re: Keeping the split token in a Java regular expression
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...Tokenizer.html Kind regards robert |
Re: Keeping the split token in a Java regular expression
On 3/26/2012 11:54 AM, laredotornado wrote:
> 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? What Lew said. String[] dates = dateString.split( ", +" ); for( String date : dates ) { String temp = date.trim().toUpper(); if( temp.endsWith( "PM" ) ) { System.out.println( "Good afternoon." ); else if( temp.endsWith( "AM" ) { System.out.println( "Good morning." ); } else { System.out.println( "Good whatever." ); } } |
Re: Keeping the split token in a Java regular expression
On Monday, March 26, 2012 1:54:40 PM UTC-5, laredotornado wrote:
> Hi, > > 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? > > Thanks, - Dave Hi, I don't want to split on the comma because there could be a case where the given String is "Fri 8 PM, Sat 1, 3, and 5 PM" and in this case, I want the result to be a String array containing Fri 8 PM Sat 1, 3, and 5 PM Your continued help is appreciated, - Dave |
Re: Keeping the split token in a Java regular expression
On 3/26/2012 2:21 PM, laredotornado@gmail.com wrote:
> Hi, I don't want to split on the comma because there could be a case > where the given String is "Fri 8 PM, Sat 1, 3, and 5 PM" and in this > case, I want the result to be a String array containing > > Fri 8 PM Sat 1, 3, and 5 PM You might be able to do this with clever use of regex look-around: http://www.regular-expressions.info/lookaround.html Maybe something like "(?<=M),". Definitely take some time to test that carefully though. Otherwise, you'll have to write your own parser (which wouldn't be hard). |
Re: Keeping the split token in a Java regular expression
On 3/26/2012 2:21 PM, laredotornado@gmail.com wrote:
> On Monday, March 26, 2012 1:54:40 PM UTC-5, laredotornado wrote: >> Hi, >> >> 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? >> >> Thanks, - Dave > > Hi, I don't want to split on the comma because there could be a case where the given String is "Fri 8 PM, Sat 1, 3, and 5 PM" and in this case, I want the result to be a String array containing > > Fri 8 PM > Sat 1, 3, and 5 PM > > Your continued help is appreciated, - Dave public class test { public static void main(String[] args) { String str = "Fri 7:30 PM, Fri 8 PM, Sat 1, 3, and 5 PM"; String token = "PM, |PM"; String[] strs = str.split(token); for (String s : strs) System.out.println(s+"PM"); } } C:\Documents and Settings\Knute Johnson>java test Fri 7:30 PM Fri 8 PM Sat 1, 3, and 5 PM If you wanted to get AMs too, you could do a first pass for the PMs and then do it again for the AMs. -- Knute Johnson |
Re: Keeping the split token in a Java regular expression
On 3/26/2012 3:56 PM, Knute Johnson wrote:
> String str = "Fri 7:30 PM, Fri 8 PM, Sat 1, 3, and 5 PM"; .... > System.out.println(s+"PM"); ^^ What does this print if the "str" string ends with AM instead of PM? I don't think this actually works.... |
Re: Keeping the split token in a Java regular expression
laredotornado <laredotornado@zipmail.com> writes:
>What I would like to do is split the expression wherever I have an public class Main { public static void split ( final java.lang.String text ) { java.util.regex.Pattern pattern = java.util.regex.Pattern.compile ( ".*?(?:am|pm),?", java.util.regex.Pattern.CASE_INSENSITIVE ); java.util.regex.Matcher matcher = pattern.matcher( text ); while( matcher.find() ) java.lang.System.out.println( matcher.group( 0 )); } public static void main( final java.lang.String[] args ) { split( "Fri 7:30 PM, Sat 2 PM, Sun 2:30 PM" ); }} |
Re: Keeping the split token in a Java regular expression
Stefan Ram wrote:
> laredotornado writes: >>What I would like to do is split the expression wherever I have an > > public class Main > { > public static void split > ( final java.lang.String text ) > { java.util.regex.Pattern pattern = > java.util.regex.Pattern.compile > ( ".*?(?:am|pm),?", java.util.regex.Pattern.CASE_INSENSITIVE ); > java.util.regex.Matcher matcher = pattern.matcher( text ); > while( matcher.find() ) > java.lang.System.out.println( matcher.group( 0 )); } > > public static void main( final java.lang.String[] args ) > { split( "Fri 7:30 PM, Sat 2 PM, Sun 2:30 PM" ); }} This excellent (except for layout) example deserves to be archived. -- Lew |
| All times are GMT. The time now is 04:04 PM. |
Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.