I wanted to get list of numbers from sequence of characters(that is: letters and digits). So I've written this code:
class A {
public static void main(String[] args) {
String msg = "aa811b22";
String[] numbers = msg.split("\\D+");
for (int i = 0; i < numbers.length; i++) {
System.out.println(">" + numbers[i] + "<");
}
}
}
Surpassingly it runs...:
$ java A
><
>811<
>22<
Ok, so somehow it matched empty string...I explained to myself that ""(empty string) actually matches regexp of NON DIGIT MATCHER so \D+. Nothing is NOT digit...right? (however... why it returned only 1 empty string? There is infinite (∞) number of empty string inside any string)
To ensure myself I tried to extract words from string given above:
class A {
public static void main(String[] args) {
String msg = "aa811b22";
String[] words = msg.split("\\d+");
for (int i = 0; i < words.length; i++) {
System.out.println(">" + words[i] + "<");
}
}
}
which actually prints what I expected (no empty strings returned):
$ run A
>aa<
>b<
but... I did few more tests that completely confused me:
System.out.println("a".split("\\D+").length);
#=> 0 (WHY NOT 1? Empty string shouldn't be here?!)
System.out.println("a1".split("\\D+").length);
#=> 2 (So now it splits to empty string and 1)
System.out.println("1a".split("\\D+").length);
#=> 1 (now it returns expected "a" string)
So my questions are:
- Why split returns empty string with my given examples?
- Why
"a".split("\\D+").lengthreturns 0 ? - why
"a1".split("\\D+").lengthis 2 (but no one) - how
"1a".split("\\D+").length)varies from"a1".split("\\D+").length)in case of splitting?