This is topic Ruby Question: in forum Books, Films, Food and Culture at Hatrack River Forum.


To visit this topic, use this URL:
http://www.hatrack.com/ubb/main/ultimatebb.php?ubb=get_topic;f=2;t=048909

Posted by Phanto (Member # 5897) on :
 
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?
 
Posted by TomDavidson (Member # 124) on :
 
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 ]
 
Posted by Phanto (Member # 5897) on :
 
Makes sense; thanks.
 
Posted by Bokonon (Member # 480) on :
 
It's actually a regular expression, which is language agnostic. Definitely useful to be familiar with regardless of your language.

-Bok
 
Posted by fugu13 (Member # 2859) on :
 
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.
 
Posted by Qaz (Member # 10298) on :
 
/Y(e(s|p|a+h))\sm(o|a)n\./
 
Posted by HollowEarth (Member # 2586) on :
 
What fugu means is don't do things like at the bottom of this page.
 
Posted by ricree101 (Member # 7749) on :
 
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]
 
Posted by Mike (Member # 55) on :
 
Yeaaaaaaaah mon.
 
Posted by anti_maven (Member # 9789) on :
 
Eeeek! Regex were mylife. I am better now.

Hoo(rah|ray)
 
Posted by fugu13 (Member # 2859) on :
 
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] .
 


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