package com.mindprod.example;
import java.util.regex.Pattern;
import static java.lang.System.*;
/**
* Test Regex Pattern.split.
*
* @author Roedy Green, Canadian Mind Products
* @version 1.1 2011-11-13 initial version
* @since 2011
*/
public class TestRegexSplit
{
/**
* main method
*
* @param args not used
*/
public static void main( String[] args )
{
{
final Pattern splitter = Pattern.compile( "\\|" );
final String what = "||abc|def||ghi||";
out.println( "Splitting " + what + " on |" );
final String[] pieces = splitter.split( what );
for ( int i = 0; i < pieces.length; i++ )
{
out.println( i + ". [" + pieces[ i ] + "]" );
}
out.println( "Lead end embedded empty fields preserved, but not trailing." );
}
{
final Pattern splitter = Pattern.compile( "\\r\\n" );
final String what = "\r\n\r\nabc\r\ndef\r\n\r\nghi\r\n\r\n";
out.println( "\nSplitting similar newline-embedded string on: " + splitter.toString() );
final String[] pieces = splitter.split( what );
for ( int i = 0; i < pieces.length; i++ )
{
out.println( i + ". [" + pieces[ i ] + "]" );
}
out.println( "Same pattern holds true for multichar \\r\\n splitter." );
}
{
final Pattern splitter = Pattern.compile( "\\|" );
final String what = "||abc|def||ghi||";
out.println( "\nSplitting " + what + " on | with unlimited split(,-1)" );
final String[] pieces = splitter.split( what, -1 );
for ( int i = 0; i < pieces.length; i++ )
{
out.println( i + ". [" + pieces[ i ] + "]" );
}
out.println( "Lead, embedded and trailing empty fields preserved." );
}
{
final Pattern splitter = Pattern.compile( "\\|" );
out.println( "\nSplitting empty string on |" );
final String[] pieces = splitter.split( "" );
for ( int i = 0; i < pieces.length; i++ )
{
out.println( i + ". [" + pieces[ i ] + "]" );
}
out.println( "Results in single empty new String[1] field, not new String[0]." );
}
{
final Pattern splitter = Pattern.compile( "\\|" );
out.println( "\nSplitting empty string on | unlimited split(,-1)" );
final String[] pieces = splitter.split( "", -1 );
for ( int i = 0; i < pieces.length; i++ )
{
out.println( i + ". [" + pieces[ i ] + "]" );
}
out.println( "Results in single empty new String[1] field, not new String[0]." );
}
}
}