This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
I had a disagreement with someone in a stack overflow thread. I'm not going to continue discussing it in the comments. It's not a forum and place to be discussing style. (The disagreement is actually about whether my answer is misleading but that's another point.)
The question is which of these is clearer. I'm curious what others think.
I think the former is clearer because it makes the precedence explicit. I think the later is shorter and slightly easier to read if you know what | does in a regex. I don't think it is a huge difference, but I'd like to get some opinions. Especially if I'm doing something to make things clearer that isn't actually clearer.
Anyway, I don't think that there is a deference between two lines, when we use split method and also replace methods. But there is a capturing group in the former and no need to think about that when using those methods.
With a regular expression this simple it makes very little difference how one writes it. When the terms are as short as this I favour the form without the braces but I use braces when the terms are more than few characters long. When the regular expression gets long I split it something along the lines of
and when very long I define the term individually along the lines of
I used this divide-and-conquer approach here to encourage the OP to create a maintainable regular expression.
Small hint - make those Strings final. That way the String concatenation will be done by the compiler, which gives you readability in code but performance in execution.
If you look at the example I posted in http://www.coderanch.com/t/622560/java/java/Parsing-log-file-spaces-strings you will find that I made them final. I also made the Pattern static because if one considers performance then whenever the Pattern is constant one should compile it only once and this implies one should use use Pattern.split() and not String.split(). Some would argue that this is premature optimisation but what the heck.
author & internet detective