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 reply to Re: Translation of reg expression by hanenkamp
in thread Translation of reg expression by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.