more useful options | |
PerlMonks |
Re: Translation of reg expressionby hanenkamp (Pilgrim) |
on Dec 31, 2003 at 15:41 UTC ( [id://317911]=note: print w/replies, xml ) | Need Help?? |
First, I would suggest consulting the appropriate documentation: perlrequick, perlretut, and perlre. Now, the period will typically match any character except newline ("\n"). This includes all punctuation. Therefore, it looks like the expression should match any email address, but it won't. The problem is that the first .+ will greedily gobble up everything and then try to match @ (which would have been gobbled up if it were present) and return false. A better solution might be: [^@]+@[^\.]+\..+This will now match any email address you feed it as the first [^@]+ matches one or more of anything that's not an @. Then, it matches the @. Next, it matches any number of characters that aren't periods. Then, the period. Finally, all other characters. This still is not what you want. This will also match other non-email type strings, such as: !@#$%^.&*If you really want to match an email containing only alphanumerics, then \w is probably what you are looking for. It matches any Perl word character and is essentially equivalent to [0-9a-zA-Z_]. So, to match one or more alphanumerics followed by an @ and then one or more alphanumerics followed by an . and then one or more alphanumerics. Try: \w+@\w+\.\w+This, however, is too stringent as email addresses may legally contain many other characters besides alphanumerics, might contain multiple periods after the @, etc.
In Section
Seekers of Perl Wisdom
|
|