I am new to regular expressions. I have a string named encryptId (does not contain |) and I want to append the | character after every 20 characters of this string, using the encryptId.replace/replaceAll(Regex,Pattern) function in Java. But it should never have \ at the end of the string.
Thanks for your help.
EDIT:
- The reason I want to use - replaceand- replaceAllfunctions particularly is because I have to use that in velocity template mananger. And there we can use common String functions but can't write whole java code.
- My current solution is shown below 
encryptId = encryptId.replaceAll("(.{20})","$1|"); if(encryptId.charAt(encryptId.length() - 1)=='|') { encryptId = encryptId.substring(0,encryptId.length()-1); }
I need to get rid of this if statement so that It would be just a string function.

replaceorreplaceAllparticularly? I think it'd be much easier to do using aStringBuilder.