I'm having trouble understanding the differences between square brackets [] and the vertical bar | in Regular Expressions.
For example: When I use this code in java to change the format of a String (Using |):
str = str.replaceAll("(\\d+|\\W)", "$0 ").replaceAll("\\s+", " ").trim();
From: (12+((23+ 37) * (774*435)))
To: ( 12 + ( ( 23 + 37 ) * ( 774 * 435 ) ) )
Which is the desired output.
However, when I use this code, I get a different output (Using []):
str = str.replaceAll("[\\d+\\W]", "$0 ").replaceAll("\\s+", " ").trim();
I get from: (12+((23+ 37) * (774*435)))
To: ( 1 2 + ( ( 2 3 + 3 7 ) * ( 7 7 4 * 4 3 5 ) ) )
Notice the spaces between the numbers (not desired).
So, can somebody please explain what is going on? Or what is the difference between [] and |?
Thank you.