Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re: Translation of reg expression

by hanenkamp (Pilgrim)
on Dec 31, 2003 at 15:41 UTC ( [id://317911]=note: print w/replies, xml ) Need Help??


in reply to Translation of reg expression

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.

Replies are listed 'Best First'.
Re: Re: Translation of reg expression
by hardburn (Abbot) on Dec 31, 2003 at 15:49 UTC

    . . . might contain multiple periods after the @, etc.

    Or no periods at all.

    Take a look at the regex in the Email::Valid module. (If you stare at it long enough, it starts to form a picture like those Magic Eye posters that were popular a few years back).

    ----
    I wanted to explore how Perl's closures can be manipulated, and ended up creating an object system by accident.
    -- Schemer

    : () { :|:& };:

    Note: All code is untested, unless otherwise stated

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://317911]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (5)
As of 2025-01-22 12:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Which URL do you most often use to access this site?












    Results (63 votes). Check out past polls.