Win a copy of Murach's MySQL this week in the JDBC and Relational Databases forum!
  • 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Regex vs manual implementation.

 
Ranch Hand
Posts: 806
MySQL Database Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 1952
7
Eclipse IDE Java
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:

 
Marshal
Posts: 79010
375
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
MySQL Database Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Won't you be my neighbor? - Fred Rogers. tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic