|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
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. |
|
#2
|
|||
|
|||
|
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> |
|
#3
|
|||
|
|||
|
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 --> |
|
#4
|
|||
|
|||
|
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! |
|
#5
|
|||
|
|||
|
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.
|
|
#6
|
|||
|
|||
|
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. |
|
#7
|
|||
|
|||
|
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. |
|
#8
|
|||
|
|||
|
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. |
|
#9
|
|||
|
|||
|
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. |
|
#10
|
|||
|
|||
|
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"; |
|
#11
|
|||
|
|||
|
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.
|
|
#12
|
|||
|
|||
|
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. |
|
#13
|
|||
|
|||
|
Re: Email example
testing only
|
|
#14
|
|||
|
|||
|
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 |
|
#15
|
|||
|
|||
|
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 --> |
![]() |
| Viewing: Dev Shed Forums > Other > Development Articles > So What's A $#!%% Regular Expression, Anyway?! |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|