• 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

writing readable regular expressions

 
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I wrote a (simplified) US phone number validation both with and without regular expressions as a blog post. i was surprised at how much code the non regular expression version was. It's been a while since I've tried to do so "the long way"
 
Bartender
Posts: 322
24
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I like that. I had to do something similar long hand in C++ last term as practice on operator >> overloading - but with the additional validation that area code and exchange could not begin with 0 or 1.
Your regex is so much prettier. Thanks for sharing it's elegance.

I must be blind, but is there a blog subscription button on your site?
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Slightly off-topic since it's not germane to what you intended to show with this example: There are so many ways in which people commit typos, or think something could/should be written, that I think it's preferable to allow anything that can reasonable be interpreted as correct data. In this case that would mean to remove anything from the input string that's not a digit, and then to check I'm left with a 10 character long string.

And, of course, you could make the regexp a lot harder to comprehend by making it self-commenting. I think that would be something like "(?# 3 digits)\\d{3}" for the first one. I have never seen the comment syntax used, but it would be interesting to hear from the person who thought this would make it easier to read :-)

@Chris: Check the page source for a few RSS feeds options.
 
Chris Barrett
Bartender
Posts: 322
24
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ulf Dittmer wrote:@Chris: Check the page source for a few RSS feeds options.



Thanks Ulf. I did see that. It's just... RSS feeds are so 1990s... I was looking for the "subscribe and get as an email" button.
Anyhow, I've added the RSS feed to Safari. All is good.
 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Huh, I do everything I can to get rid of such emails, and use RSS wherever I can :-)
 
Bartender
Posts: 2407
36
Scala Python Oracle Postgres Database Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
+1 for your divide-and-conquer approach to the RE here: I'll try to remember that next time I'm in Regex Hell!
 
reply
    Bookmark Topic Watch Topic
  • New Topic