I
Ichiro
Hi there,
I wrote a test harness for java.util.regex.Pattern.split and found
that, at least from my point of view, it behaves inconsistently. See
program and output below.
In particular, a trailing delimiter does not generate an empty string
on the right of the delimiter, so I get
*******************
'a,' splits into:
'a'
*******************
while I expected
*******************
'a,' splits into:
'a'
''
*******************
Also, possibly even more bizarrely
*******************
',' splits into:
*******************
while (since the empty string splits into an empty string) I expected
*******************
',,' splits into:
''
''
*******************
I tried to modify my pattern to also take beginning and end of string
into account, like so
pattern = Pattern.compile("[\\A\\z,]");
but this generated a PatternSyntaxException.
Can someone please suggest a way to achieve what I need?
Finally, is this the right newsgroup for this kind of questions? There
sure is a lot of noise (=spam) around here...
Thanks much,
Ichiro
import java.util.regex.Pattern;
public class Main
{
public static void main(String[] args)
{
String input;
input = "test";
printSplit(input);
input = "";
printSplit(input);
input = "a,b";
printSplit(input);
input = "a,";
printSplit(input);
input = ",b";
printSplit(input);
input = ",";
printSplit(input);
input = "a,b,c";
printSplit(input);
input = "a,b,";
printSplit(input);
input = "a,,c";
printSplit(input);
input = ",b,c";
printSplit(input);
input = "a,,";
printSplit(input);
input = ",b,";
printSplit(input);
input = ",,c";
printSplit(input);
input = ",,";
printSplit(input);
}
private static void printSplit(String input)
{
Pattern pattern;
String[] output;
pattern = Pattern.compile(",");
output = pattern.split(input);
System.out.println("'" + input + "' splits into:");
for (String s : output)
{
System.out.println("'" + s + "'");
}
System.out.println("*******************");
}
}
'test' splits into:
'test'
*******************
'' splits into:
''
*******************
'a,b' splits into:
'a'
'b'
*******************
'a,' splits into:
'a'
*******************
',b' splits into:
''
'b'
*******************
',' splits into:
*******************
'a,b,c' splits into:
'a'
'b'
'c'
*******************
'a,b,' splits into:
'a'
'b'
*******************
'a,,c' splits into:
'a'
''
'c'
*******************
',b,c' splits into:
''
'b'
'c'
*******************
'a,,' splits into:
'a'
*******************
',b,' splits into:
''
'b'
*******************
',,c' splits into:
''
''
'c'
*******************
',,' splits into:
*******************
I wrote a test harness for java.util.regex.Pattern.split and found
that, at least from my point of view, it behaves inconsistently. See
program and output below.
In particular, a trailing delimiter does not generate an empty string
on the right of the delimiter, so I get
*******************
'a,' splits into:
'a'
*******************
while I expected
*******************
'a,' splits into:
'a'
''
*******************
Also, possibly even more bizarrely
*******************
',' splits into:
*******************
while (since the empty string splits into an empty string) I expected
*******************
',,' splits into:
''
''
*******************
I tried to modify my pattern to also take beginning and end of string
into account, like so
pattern = Pattern.compile("[\\A\\z,]");
but this generated a PatternSyntaxException.
Can someone please suggest a way to achieve what I need?
Finally, is this the right newsgroup for this kind of questions? There
sure is a lot of noise (=spam) around here...
Thanks much,
Ichiro
import java.util.regex.Pattern;
public class Main
{
public static void main(String[] args)
{
String input;
input = "test";
printSplit(input);
input = "";
printSplit(input);
input = "a,b";
printSplit(input);
input = "a,";
printSplit(input);
input = ",b";
printSplit(input);
input = ",";
printSplit(input);
input = "a,b,c";
printSplit(input);
input = "a,b,";
printSplit(input);
input = "a,,c";
printSplit(input);
input = ",b,c";
printSplit(input);
input = "a,,";
printSplit(input);
input = ",b,";
printSplit(input);
input = ",,c";
printSplit(input);
input = ",,";
printSplit(input);
}
private static void printSplit(String input)
{
Pattern pattern;
String[] output;
pattern = Pattern.compile(",");
output = pattern.split(input);
System.out.println("'" + input + "' splits into:");
for (String s : output)
{
System.out.println("'" + s + "'");
}
System.out.println("*******************");
}
}
'test' splits into:
'test'
*******************
'' splits into:
''
*******************
'a,b' splits into:
'a'
'b'
*******************
'a,' splits into:
'a'
*******************
',b' splits into:
''
'b'
*******************
',' splits into:
*******************
'a,b,c' splits into:
'a'
'b'
'c'
*******************
'a,b,' splits into:
'a'
'b'
*******************
'a,,c' splits into:
'a'
''
'c'
*******************
',b,c' splits into:
''
'b'
'c'
*******************
'a,,' splits into:
'a'
*******************
',b,' splits into:
''
'b'
*******************
',,c' splits into:
''
''
'c'
*******************
',,' splits into:
*******************