Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Regex vs manual implementation.

 
chaitanya karthikk
Ranch Hand
Posts: 806
Java MySQL Database Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all, I would like to know which one is faster. A regex or writing our own validation code?

For example I have a string which has some tabs at the start. I want to find the number of beginning tabs or spaces.

Simply writing a for loop and using the charAt() method is quick or regex is quick? And can anyone please explain why?

I have read a post here which says that iterating through the text and finding the spaces and tabs is quick instead of using regex.

If iterating using charAt is quick, then what if I want to validate an email?

1. Check whether the first character is an alphabet or not. If alphabet then
2. Simply iterate through the email to check if it has an '@' character. While iterating using charAt, I will append the chars to a StringBuffer. When I find '@' then I will append the rest characters to another StringBuffer. I want to skip split() (or) substring() methods. If '@' character is found then
4. Then iterate the first string whether it has any special characters other than _ and .(dot). If valid then
5. Check the second string does not have any special characters other than .(dot)

Suppose if this is how I am going to validate an email id. Then is this quick than a regex?

Thank you all in advance. Good day.
 
Jelle Klap
Bartender
Posts: 1952
7
Eclipse IDE Java
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Strive for clarity first and foremost, not micro-optimizations in speed.
If a simple regex can prevent you from having to write a convoluted mess of loops and conditions, go with the regex approach. On the otherhand, if a regex becomes impossibly hard to understand, you might want to reconsider the regex approach - example:

 
Campbell Ritchie
Sheriff
Pie
Posts: 49733
69
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The nice thing about regexes is that you may be able to search and find a regex already made. For validating internet e-mail addresses, you can use a simple little regex which will fit easily into two pages
 
fred rosenberger
lowercase baba
Bartender
Posts: 12180
34
Chrome Java Linux
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
chaitanya karthikk wrote:Hi all, I would like to know which one is faster. A regex or writing our own validation code?

Faster in terms of what? Writing the code? execution? Debugging?

Look at it this way. If you are going to run the validation a million times, the difference between the two might be as much as a second.

Writing one vs the other will more than make up that time. And, if you have to go back and debug it later, one may take hours to fix, while the other only a few minutes.

I'm not saying which is which, since it depends on what you are more familiar with. But choosing one vs. the other based on execution time is pointless. You've spent more time worrying about that than you will ever save by choosing the 'faster' one.
 
chaitanya karthikk
Ranch Hand
Posts: 806
Java MySQL Database Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you all. I just wanted to know how iteration is fast than using regex. I dint say that I am going to use this approach. Definitely a single line code is easily understood than writing a bunch of lines to make it execute quickly. I was curious. So I asked this question.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic