FacebookTwitter
Hatrack River Forum   
my profile login | search | faq | forum home

  next oldest topic   next newest topic
» Hatrack River Forum » Active Forums » Books, Films, Food and Culture » Ruby Question:

   
Author Topic: Ruby Question:
Phanto
Member
Member # 5897

 - posted      Profile for Phanto           Edit/Delete Post 
I've been learning Ruby and making good progress. The manual from which I've been learning, however, gave me a more cryptic piece of code than usual:

quote:

phone_re = /\((\d{3})\)\s+(\d{3})-(\d{4})/

Source

The problem is that while I get the rest of this program, this line I don't, and I can't seem to find information elsewhere on it. Any Ruby guru here would be kind enough for a quick answer?

Posts: 3060 | Registered: Nov 2003  |  IP: Logged | Report this post to a Moderator
TomDavidson
Member
Member # 124

 - posted      Profile for TomDavidson   Email TomDavidson         Edit/Delete Post 
It's an input mask for a phone number using regular expressions. The first three digits are surrounded by parentheses (which have been escaped out with backslashes), and the \s is a check for whitespace. The phone number is divided into three groups -- area code, three digits, and four digits -- by non-escaped parentheses, and the \d{#} indicates how many digits are in each group.

[ June 11, 2007, 08:54 PM: Message edited by: TomDavidson ]

Posts: 37449 | Registered: May 1999  |  IP: Logged | Report this post to a Moderator
Phanto
Member
Member # 5897

 - posted      Profile for Phanto           Edit/Delete Post 
Makes sense; thanks.
Posts: 3060 | Registered: Nov 2003  |  IP: Logged | Report this post to a Moderator
Bokonon
Member
Member # 480

 - posted      Profile for Bokonon           Edit/Delete Post 
It's actually a regular expression, which is language agnostic. Definitely useful to be familiar with regardless of your language.

-Bok

Posts: 7021 | Registered: Nov 1999  |  IP: Logged | Report this post to a Moderator
fugu13
Member
Member # 2859

 - posted      Profile for fugu13   Email fugu13         Edit/Delete Post 
On a more general note, while regular expressions are powerful and useful, they suffer from 'everything looks a nail' syndrome.

For instance, if parsing user submitted phone numbers is what's in question, it is better to normalize by stripping all non-numeric digits, then do a check against the requirements for valid phone numbers (which can just be a length check in the naive case), rather than requiring users to submit in a particular pattern.

Regular expressions are also frequent sources of errors in programs, particularly for beginners. Making the logic involved more explicit can make a program much more maintainable, as well.

This isn't to say there aren't excellent times to use regular expressions, just that it is very easy to over-use them.

Posts: 15770 | Registered: Dec 2001  |  IP: Logged | Report this post to a Moderator
Qaz
Member
Member # 10298

 - posted      Profile for Qaz           Edit/Delete Post 
/Y(e(s|p|a+h))\sm(o|a)n\./
Posts: 544 | Registered: Mar 2007  |  IP: Logged | Report this post to a Moderator
HollowEarth
Member
Member # 2586

 - posted      Profile for HollowEarth   Email HollowEarth         Edit/Delete Post 
What fugu means is don't do things like at the bottom of this page.
Posts: 1621 | Registered: Oct 2001  |  IP: Logged | Report this post to a Moderator
ricree101
Member
Member # 7749

 - posted      Profile for ricree101   Email ricree101         Edit/Delete Post 
quote:
Originally posted by HollowEarth:
What fugu means is don't do things like at the bottom of this page.

I don't know. The regular expression seems decent for what it's being used for, which is simply expressing a standard. No one is suggesting that it should be a practical implementation (I hope).

Still, that was an impressively long and cryptic expression. Sounds like it belongs somewhere in the International Obfuscated C contest. [Smile]

Posts: 2437 | Registered: Apr 2005  |  IP: Logged | Report this post to a Moderator
Mike
Member
Member # 55

 - posted      Profile for Mike   Email Mike         Edit/Delete Post 
Yeaaaaaaaah mon.
Posts: 1810 | Registered: Jan 1999  |  IP: Logged | Report this post to a Moderator
anti_maven
Member
Member # 9789

 - posted      Profile for anti_maven   Email anti_maven         Edit/Delete Post 
Eeeek! Regex were mylife. I am better now.

Hoo(rah|ray)

Posts: 892 | Registered: Oct 2006  |  IP: Logged | Report this post to a Moderator
fugu13
Member
Member # 2859

 - posted      Profile for fugu13   Email fugu13         Edit/Delete Post 
I suspect a properly written parser would be better and faster at validating a standard, as well as considerably more readable for understanding it [Wink] .
Posts: 15770 | Registered: Dec 2001  |  IP: Logged | Report this post to a Moderator
   

   Close Topic   Feature Topic   Move Topic   Delete Topic next oldest topic   next newest topic
 - Printer-friendly view of this topic
Hop To:


Contact Us | Hatrack River Home Page

Copyright © 2008 Hatrack River Enterprises Inc. All rights reserved.
Reproduction in whole or in part without permission is prohibited.


Powered by Infopop Corporation
UBB.classic™ 6.7.2