For input String id, I want to do 4 steps like below :
- Remove all not lowercase alphabet, number, "-", "_", "."
- If "." is multiple and continuous, replace it to single "." (ex: he......llo -> he.llo)
- If String start with ".", remove it.
- If String ends with ".", remove it.
And Here is 4 Line of my code :
id = id.replaceAll("[^" + "a-z" + "0-9" + "-" + "_" + "." + "]", "");
id = id.replaceAll(".{2,}",".");
id = id.replaceAll("^.","");
id = id.replaceAll(".$","");
I found the return of rule 2 will be "." (ex : he...llo -> .) and rule 3,4 will remove string which is not "."
So I fix the code like :
id = id.replaceAll("[^" + "a-z" + "0-9" + "-" + "_" + "." + "]", "");
id = id.replaceAll("\\.{2,}",".");
id = id.replaceAll("\\^.","");
id = id.replaceAll("\\.$","");
And it works fine. I just don't understand. Is that regular expression need to add "\" twice before it uses? If it is right, why rule 1 work just fine? Who can get me right answer specifically? at last, I wonder can I code rule 3 and rule 4 at once? like using && to ?