Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I would like to interpret this reg expresssion:
^[\w.-]+\@(?:[\w-]+\.)+\w+$/
It says start with any characters then get a "@" then match word and/or period then has to end with a word??

Is this correct? Also not sure about the parenthesis and brackets and the negative dashes. I looked up the "\w" which says word characters so I understand that but the "+" and "-" and "?:" and what the brackets and paranthesis are all not clear to me.

Replies are listed 'Best First'.
Re: Reqular Expression Explanation
by broquaint (Abbot) on Nov 04, 2002 at 12:28 UTC
    For all your regexp explanations see. japhy's YAPE::Regex::Explain
    shell> perl -MYAPE::Regex::Explain -e \ 'print YAPE::Regex::Explain->new(q{^[\w.-]+\@(?:[\w-]+\.)+\w+$})->expl +ain()' The regular expression: (?-imsx:^[\w.-]+\@(?:[\w-]+\.)+\w+$) matches as follows: NODE EXPLANATION ---------------------------------------------------------------------- (?-imsx: group, but do not capture (case-sensitive) (with ^ and $ matching normally) (with . not matching \n) (matching whitespace and # normally): ---------------------------------------------------------------------- ^ the beginning of the string ---------------------------------------------------------------------- [\w.-]+ any character of: word characters (a-z, A- Z, 0-9, _), '.', '-' (1 or more times (matching the most amount possible)) ---------------------------------------------------------------------- \@ '@' ---------------------------------------------------------------------- (?: group, but do not capture (1 or more times (matching the most amount possible)): ---------------------------------------------------------------------- [\w-]+ any character of: word characters (a-z, A-Z, 0-9, _), '-' (1 or more times (matching the most amount possible)) ---------------------------------------------------------------------- \. '.' ---------------------------------------------------------------------- )+ end of grouping ---------------------------------------------------------------------- \w+ word characters (a-z, A-Z, 0-9, _) (1 or more times (matching the most amount possible)) ---------------------------------------------------------------------- $ before an optional \n, and the end of the string ---------------------------------------------------------------------- ) end of grouping ----------------------------------------------------------------------

    HTH

    _________
    broquaint

Re: Reqular Expression Explanation
by Tanalis (Curate) on Nov 04, 2002 at 12:27 UTC
    The regexp is basically saying:

    ^ - at the start of the line [\w.-]+ - match one or more word characters, periods or hyphens \@ - followed by an @ (?:[\w-]+\.)+ - then a series of either one or more word characters/hy +phens or a period \w+ - followed by one or more word characters $ - then the end of line.

    It looks like this is to match e-mail addresses in text .. something of the format email.address@my.email.com.

    Hope that helps ..
    --Foxcub

Re: Reqular Expression Explanation
by fglock (Vicar) on Nov 04, 2002 at 12:29 UTC

    It looks like a mail address validation:

    ^ line-start. [\w.-]+ a word-char, a dot or a minus, at least once. \@ a '@'. (?:[\w-]+\.)+ a word-char, a minus, at least once, followed by a dot. Repeat all that. \w+ some word-chars. $ end-of-line.
      Thanks to all of you!!!
Re: Reqular Expression Explanation
by helgi (Hermit) on Nov 04, 2002 at 13:35 UTC
    It looks as if you are trying to validate an e-mail address, so perhaps you should have a look at:

    perldoc -q How do I check a valid mail address?

    And then give it up for a lost cause in favour of more fruitful endeavors.

    --
    Regards,
    Helgi Briem
    helgi AT decode DOT is

Re: E-Mail Reqular Expression Explanation
by tadman (Prior) on Nov 04, 2002 at 18:44 UTC
    I think it's important to note that this regular expression is only an approximation of correct. For a more reliable check, you should probably use Email::Valid to verify:
    use Email::Valid; unless (Email::Valid->address($email_address)) { warn "Not a valid e-mail address\n"; return; }
    There are a lot of things that can appear in e-mail addresses apart from just letters, numbers, dashes and dots.
Re: Reqular Expression Explanation
by Anonymous Monk on Nov 04, 2002 at 12:15 UTC
    This is how its supose to look, I took out the slash at the end:
    ^[\w.-]+\@(?:[\w-]+\.)+\w+$