Development Articles
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsOtherDevelopment Articles

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old April 13th, 2000, 04:51 PM
guest
Guest
Dev Shed Newbie (0 - 499 posts)
 
Posts: n/a  
Time spent in forums:
Reputation Power:
Nice Article, just one question on examples

This is a very nice and informative article, thanks. The Oreilly regex book is good to for those that want more info.

Anyway, I am perplexed by the first two "simple" examples:

<CITE>
"*" and "?" - these are used to match zero or more occurrences of the preceding character, and zero or one occurrence of the preceding character, respectively. So,

/eg*/

would match "easy", "egocentric" and "egg"

while

/Wil?/

would match "Winnie", "Wimpy" "Wilson" and "William", though not "Wendy" or "Wolf".
</CITE>

My questions are either I am missing something fundamental or these examples are wrong. I am not a regex expert, but why does the first example /eg*/ match "easy" and why does the second example /Wil?/ match "Winnie" and "Wimpy"? By the aforementioned definition these would not match. Am I crazy or missing something?

Help, thanks.

Reply With Quote
  #2  
Old April 13th, 2000, 06:08 PM
guest
Guest
Dev Shed Newbie (0 - 499 posts)
 
Posts: n/a  
Time spent in forums:
Reputation Power:
Re: Nice Article, just one question on examples

The examples are correct.
<br>
<br>
The '*' character means zero or more of the PRECEEDING item. That's why /eg*/ matches 'easy', because 'easy' has exactly 1 'e' followed by zero 'g's. Likewise the /Wil?/ can match 'Wimpy' because the 'Wi' in 'Wimpy' matches the 'Wi' in the expression, and is followed by zero 'l's and the '?' character permits zero items to match.
<br>
<br>
Perhaps the examples aren't the best, because /eg*/ is exactly the same as just doing /e/ since with or without a 'g' both match anything with an 'e' in it. Likewise the /Wil?/ expression is essentially the same as doing /Wi/ because with or without an 'l' both will match anything with a 'Wi' in them.
<br>
<br>
There is a slight difference between my above "equivilent" expressions however. While 'Wilting' matches both /Wil?/ and /Wi/, the portion of 'Wilting' that is matched is different. So both expressions return TRUE in the sense that a match did occur, the contents of that match different.
<br>
<br>
Clear as mud?
<br>
<br>

Reply With Quote
  #3  
Old April 13th, 2000, 06:24 PM
guest
Guest
Dev Shed Newbie (0 - 499 posts)
 
Posts: n/a  
Time spent in forums:
Reputation Power:
Email example

I think there's a problem with the email regex, particularly, the last section:

<!-- Code -->
<p><pre><font color=#008000><xmp>(\.[a-zA-Z0-9_-])+</xmp></font></pre><p>
<!-- Code -->


After the @ sign, you can't match whole words past the first one.

There should be a + inside the right-hand parenthesis. As it stands now, the regex matches
<!-- Code -->
<p><pre><font color=#008000><xmp>mejico@kibltuf.a.b.c</xmp></font></pre><p>
<!-- Code -->
but not
<!-- Code -->
<p><pre><font color=#008000><xmp>mejico@kibltuf.whatisthis.com</xmp></font></pre><p>
<!-- Code -->
because the repeated sequence is the period and the "alphanumeric" character.

This should work better:
<!-- Code -->
<p><pre><font color=#008000><xmp>/^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-]+)+/</xmp></font></pre><p>
<!-- Code -->

Reply With Quote
  #4  
Old April 13th, 2000, 06:27 PM
guest
Guest
Dev Shed Newbie (0 - 499 posts)
 
Posts: n/a  
Time spent in forums:
Reputation Power:
Re: Email example

Whoops, that should be:

<!-- Code -->
<p><pre><font color=#008000><xmp>/^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-]+)+/</xmp></font></pre><p>
<!-- Code -->

Forgot to escape the period!

Reply With Quote
  #5  
Old April 13th, 2000, 06:56 PM
guest
Guest
Dev Shed Newbie (0 - 499 posts)
 
Posts: n/a  
Time spent in forums:
Reputation Power:
Re: Email example

Email addresses (according to the RFCs) can be very, very complex beasties, so PLEASE don't use the example regular expression(s) in the article for testing the validity of an email address. They're most likely meant as educational tools only.

Reply With Quote
  #6  
Old April 14th, 2000, 03:43 AM
guest
Guest
Dev Shed Newbie (0 - 499 posts)
 
Posts: n/a  
Time spent in forums:
Reputation Power:
Re: Email example

Your example's absolutely correct - as we've stressed in the article, the regex used to validate email addresses there is a very basic illustration, and would need to be fine-tuned for a live site.

BTW - the regex that we have used *will* work on addresses of the form
<code>
mejico@kibltuf.whatisthis.com
</code>

Our purpose in that section of the article was more to illustrate how to include regexes in Perl, PHP and JavaScript code, and less on creating a regex that matched all possible address formats.

Hope that clarifies the issue.

Reply With Quote
  #7  
Old April 14th, 2000, 03:43 AM
guest
Guest
Dev Shed Newbie (0 - 499 posts)
 
Posts: n/a  
Time spent in forums:
Reputation Power:
Re: Email example

Your example's absolutely correct - as we've stressed in the article, the regex used to validate email addresses there is a very basic illustration, and would need to be fine-tuned for a live site.

BTW - the regex that we have used *will* work on addresses of the form
<code>
mejico@kibltuf.whatisthis.com
</code>

Our purpose in that section of the article was more to illustrate how to include regexes in Perl, PHP and JavaScript code, and less on creating a regex that matched all possible address formats.

Hope that clarifies the issue.

Reply With Quote
  #8  
Old April 14th, 2000, 07:45 AM
guest
Guest
Dev Shed Newbie (0 - 499 posts)
 
Posts: n/a  
Time spent in forums:
Reputation Power:
Thanks for Article

Thanks for article - I particulary liked that you showed examples in the three scripting languages I ( and I'm sure most web developers) use daily.

Also, to Howard Bannister - thanks for your responses to the other posts. I found them helpful.

Reply With Quote
  #9  
Old April 14th, 2000, 07:09 PM
guest
Guest
Dev Shed Newbie (0 - 499 posts)
 
Posts: n/a  
Time spent in forums:
Reputation Power:
Float to Decimal(2 places)

Problem: I have a float (say) 123.456789 and I want to convert this to a two decimal place digit. This doesn't work.
$total=123.45678;
@total1 = split(/\./,$total);
@total1[1]=~s/[0-9]+/([0-9][0-9])/;
print "@total1[0].@total1[1]\n";

It returns 123.([0-9][0-9])

I've tried /\d+/\d\d/ as well. No Go.

An thoughts.

Reply With Quote
  #10  
Old April 16th, 2000, 12:19 PM
guest
Guest
Dev Shed Newbie (0 - 499 posts)
 
Posts: n/a  
Time spent in forums:
Reputation Power:
Re: Float to Decimal(2 places)

Try this perl script.

Code:
#!/usr/bin/perl

print "Enter the float number\n";
$number = <STDIN>;
chomp($number);


$number =~s/([0-9]+)\.([0-9][0-9])[0-9]+/$1\.$2/g;
print $number,"\n";


Reply With Quote
  #11  
Old April 17th, 2000, 10:36 AM
guest
Guest
Dev Shed Newbie (0 - 499 posts)
 
Posts: n/a  
Time spent in forums:
Reputation Power:
Re: Nice Article, just one question on examples

Thanks! That actually does clear it up and that extra direction is exactly what I needed. I thought that was the deal but I was confused, thanks again for the help.

Reply With Quote
  #12  
Old April 17th, 2000, 12:16 PM
guest
Guest
Dev Shed Newbie (0 - 499 posts)
 
Posts: n/a  
Time spent in forums:
Reputation Power:
Re: Email example

Vikram Vaswani and Harish Kamath:

You are right; your regex will match the sample email address I had offered, but not the entire expression.

I primarily use regular expressions to perform global "search & replace" operations in vi, and sometimes getting a perfect match for an expression, from head to tail, can be tricky.

But that's a finer point. You wrote a very good article, and I appreciate your patience with me.

Reply With Quote
  #13  
Old April 17th, 2000, 12:49 PM
guest
Guest
Dev Shed Newbie (0 - 499 posts)
 
Posts: n/a  
Time spent in forums:
Reputation Power:
Re: Email example

testing only

Reply With Quote
  #14  
Old April 17th, 2000, 01:19 PM
guest
Guest
Dev Shed Newbie (0 - 499 posts)
 
Posts: n/a  
Time spent in forums:
Reputation Power:
Re: Email example

David:

No problem at all!

Glad you liked the article, and keep the comments coming - we look forward to reading them :)

Vikram

Reply With Quote
  #15  
Old April 18th, 2000, 08:34 AM
guest
Guest
Dev Shed Newbie (0 - 499 posts)
 
Posts: n/a  
Time spent in forums:
Reputation Power:
Re: Float to Decimal(2 places)

This is not a regexp problem. The simple
solution (in perl) is:

<!-- Code -->
<p><pre><font color=#008000><xmp>
$total = 123.45678;
$total = (int($total * 100))/100;
</xmp></font></pre><p>
<!-- Code -->

Reply With Quote
Reply

Viewing: Dev Shed ForumsOtherDevelopment Articles > So What's A $#!%% Regular Expression, Anyway?!


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway
Stay green...Green IT