This week's giveaway is in the EJB and other Java EE Technologies forum.
We're giving away four copies of EJB 3 in Action and have Debu Panda, Reza Rahman, Ryan Cuprak, and Michael Remijan on-line!
See this thread for details.
The moose likes Java in General and the fly likes regular expressions - which do you find clearer? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "regular expressions - which do you find clearer?" Watch "regular expressions - which do you find clearer?" New topic
Author

regular expressions - which do you find clearer?

Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 29220
    
135

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.

[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Ramesh Pramuditha Rathnayake
Ranch Hand

Joined: Oct 31, 2012
Posts: 169
    
    1

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.


Ramesh-X
Richard Tookey
Ranch Hand

Joined: Aug 27, 2012
Posts: 958
    
  10

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

Joined: Aug 07, 2007
Posts: 1936
    
  28
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

Joined: Oct 27, 2005
Posts: 19541
    
  16

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
Ranch Hand

Joined: Aug 27, 2012
Posts: 958
    
  10

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
internet detective
Marshal

Joined: May 26, 2003
Posts: 29220
    
135

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
Ranch Hand

Joined: Aug 27, 2012
Posts: 958
    
  10

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: regular expressions - which do you find clearer?
 
Similar Threads
urgent help--- fileformatting
how to split "abc-def - ghi-jkl"?
convert C# regex into java regex
ExamLab: invalid expression String [] s = files.split("[(\\s)+]");
xpath & regexp extractor in Jmeter