Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

regular expressions - which do you find clearer?

 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34396
346
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ramesh Pramuditha Rathnayake
Ranch Hand
Posts: 178
2
Java MySQL Database Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't understand what is precedence explicit..?

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.
 
Richard Tookey
Bartender
Posts: 1166
17
Java Linux Netbeans IDE
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.



 
Tony Docherty
Bartender
Posts: 2957
59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The question is which of these is clearer. I'm curious what others think.

For such a simple regex I'd go with the later but for more complex cases I agree the parenthesis can make things a lot clearer.

I think the thing that really is clearer is how pedantic and petty some people can be and thank goodness we don't tolerate that type of attitude here.
 
Rob Spoor
Sheriff
Pie
Posts: 20527
54
Chrome Eclipse IDE Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would never choose the first option, because it adds extra overhead with the capturing group. I would probably have written (?:--)|-|\\s myself. It's basically the same without the capturing.

Richard Tookey wrote:

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.
 
Richard Tookey
Bartender
Posts: 1166
17
Java Linux Netbeans IDE
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Spoor wrote:
Richard Tookey wrote:

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.


 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34396
346
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Great comments and reasons. Thanks all.

I was stuck at "my gut likes it better: and not able to think of why. I particular like Richard's rule of thumb. In fact, I'm giving a cow for providing the example.
 
Richard Tookey
Bartender
Posts: 1166
17
Java Linux Netbeans IDE
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeanne Boyarsky wrote: In fact, I'm giving a cow for providing the example.


Thanks for the cow Jeanne. At my current rate of getting cows I will have had 27 telegrams from the queen before I reach your cow total.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic