Re: Keeping the split token in a Java regular expression
ram@zedat.fu-berlin.de (Stefan Ram) writes:
public static void main( final java.lang.String[] args )
{ split( "Fri 7:30 PM, Sat 2 PM, Sun 2:30 PM" ); }}
Thanks for the comments! I believe Jim's answer was most
close to what the OP asked for, and Robert is right with
most of his criticism.
As someone said regular expressions were too much overhead,
I tried a solution without regular expressions (with custom
pattern matching); it was not thoroughly tested, though:
final class Tracer
{ private int pos = 0;
private boolean matched = false;
private final boolean advance(){ ++this.pos; return false; }
public final boolean reset()
{ this.pos = 0; this.matched = false; return false; }
public final boolean matched(){ return this.matched; }
public final boolean accept( final char c )
{ final char ch = java.lang.Character.toLowerCase( c );
switch( pos )
{ case 0: /* the pattern is hardcoded below */
return ch == 'a' || ch == 'p' ? this.advance(): this.reset();
case 1:
if( ch == 'm' ){ this.matched = true; return true; }
else { --pos; return this.accept( c ); }
default: this.reset(); return false; }}}
final class Splitter
{
private final java.util.List<java.lang.CharSequence> target
= new java.util.ArrayList<java.lang.CharSequence>();
private final int comma
( final java.lang.CharSequence text, final int i, final int length )
{ final int j = i + 1;
return j < length ? text.charAt( j ) == ',' ? j : i : j; }
public final java.util.List<java.lang.CharSequence> split
( final java.lang.CharSequence text )
{ final Tracer tracer = new Tracer();
final int length = text.length();
int l = 0;
for( int i = 0; i < length; ++i )
{ tracer.accept( text.charAt( i ));
if( tracer.matched() )
{ i = comma( text, i, length );
this.target.add( text.subSequence( l, i ));
tracer.reset();
l = i + 1; }}
return target; }}
public final class Main
{
public static void main( final java.lang.String[] args )
{
java.lang.System.out.println
( new Splitter().split
( "Fri 7:30 PM, Sat 2 PM, Sun 2:30 PM" )); }}
[Fri 7:30 PM, Sat 2 PM, Sun 2:30 PM]