• Post Reply Bookmark Topic Watch Topic
  • New Topic

writing readable regular expressions  RSS feed

 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37465
539
Eclipse IDE Java VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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"
 
Chris Barrett
Bartender
Posts: 321
24
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • 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?
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • 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: 321
24
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • 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: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Huh, I do everything I can to get rid of such emails, and use RSS wherever I can :-)
 
chris webster
Bartender
Posts: 2407
36
Linux Oracle Postgres Database Python Scala
  • Mark post as helpful
  • send pies
  • 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!
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!