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]

I've found a solution: replaceAll('.', ',') not replaceAll(".", ",") ! does the job so then split succeeds...But still this's pretty weird....
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.
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 ...