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

I was hoping someone could tell me what this is doing in as much detail as you can??
unless $target =~ /^(?:https?|ftp):/;

Replies are listed 'Best First'.
Re: Need interpretation of line
by davorg (Chancellor) on Sep 19, 2002 at 10:39 UTC

    It checks to see if $target starts with 'http', 'https' or 'ftp', followed by ':'.

    --
    <http://www.dave.org.uk>

    "The first rule of Perl club is you do not talk about Perl club."
    -- Chip Salzenberg

      thank you for your quick answer. So the unless part is saying if $target does not start with 'http', 'https' or 'ftp', followed by a '.'?

        That's right.

        --
        <http://www.dave.org.uk>

        "The first rule of Perl club is you do not talk about Perl club."
        -- Chip Salzenberg

Re: Need interpretation of line
by flounder99 (Friar) on Sep 19, 2002 at 14:03 UTC
    A handy tool for understanding complex regexes is YAPE::Regex::Explain
    $ perl -e 'use YAPE::Regex::Explain;print YAPE::Regex::Explain->new("^ +(?:https?|ftp):")->explain;' The regular expression: (?-imsx:^(?:https?|ftp):) 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 ---------------------------------------------------------------------- (?: group, but do not capture: ---------------------------------------------------------------------- http 'http' ---------------------------------------------------------------------- s? 's' (optional (matching the most amount possible)) ---------------------------------------------------------------------- | OR ---------------------------------------------------------------------- ftp 'ftp' ---------------------------------------------------------------------- ) end of grouping ---------------------------------------------------------------------- : ':' ---------------------------------------------------------------------- ) end of grouping ----------------------------------------------------------------------

    --

    flounder