• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

regular expressions - which do you find clearer?

 
author & internet detective
Posts: 42055
926
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 178
2
Netbeans IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 1166
17
Netbeans IDE Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.



 
Bartender
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 22817
132
Eclipse IDE Spring Chrome Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Netbeans IDE Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 https://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
Posts: 42055
926
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Netbeans IDE Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Today you are you, that is turer than true. There is no one alive who is youer than you! - Seuss. Tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic