String.split() and DOT regex

Hi All,

I've discovered pretty weird behavior inString.split() function when the regex is DOT (.):

String ipAsString ="10.0.0.4";

String[] nodes = ipAsString.split(".");

The result of the previous two lines is 0 (i.e. nodes.length == 0) but if I replace DOT by COMMA (",") the function succeeds.

P.S. replaceAll() also fails to replace DOTs. (I use SDK 1.4.2_03)

Thanks a lot !

[494 byte] By [Piligrim] at [2007-9-30 4:41:27]
# 1
I've found a solution: replaceAll('.', ',') not replaceAll(".", ",") ! does the job so then split succeeds...But still this's pretty weird....
Piligrim at 2007-7-1 14:25:11 > top of Java-index,Administration Tools,Sun Connection...
# 2

The period/dot is a special character in Regular Expressions and has the meaning "any character". If you want to split around the dot, you need to use the expression "\\." By escaping the dot "\." a reference to the dot is created, as opposed to the any character meaning; the backslask then needs to be repeated "\\." because Java will strip a backslash before passing the expression to the Regex engine.

ChuckBing at 2007-7-1 14:25:11 > top of Java-index,Administration Tools,Sun Connection...
# 3

I found this solution:

example:

String myString = "1.4.2_05";

String[] mySpitString = myString.split("\\.");

result:

mySpitString[0]=1

mySpitString[1]=4

mySpitString[2]=2_05

I think that the first back slash protect the escape sequence of regex parser "\." and the second back slash is the regex parser escape sequence for protect the dot ...

mv_serinf at 2007-7-1 14:25:11 > top of Java-index,Administration Tools,Sun Connection...
# 4
That is what I wrote in my reply immediately preceeding yours...
ChuckBing at 2007-7-1 14:25:11 > top of Java-index,Administration Tools,Sun Connection...