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

Can i confirm the following:

1. This code is just looking for any number of backslashes

if ($var ~= m/\*/)
2. This code is matching for a backslash but what does the $ mean
unless($path ~= m/\/$/)
3. This code is replacing . gz at the end of a string. with nothing
$output ~= s/\.gz$//;
Many thanks

Replies are listed 'Best First'.
Re: Basic regular expressions
by mjscott2702 (Pilgrim) on Oct 21, 2010 at 12:53 UTC
    1. Is looking for at least 1 asterisk (the * is escaped here)

    2. Is looking for a forward slash at the end of the $path string i.e. anchored by the $

    3. Correct, the . metacharacter is escaped, so remove .gz from the end of the string

Re: Basic regular expressions
by Corion (Patriarch) on Oct 21, 2010 at 12:55 UTC

    No.

    In detail:

    1. No, see perlre on what the backslash does
    2. No, see perlre on what the backslash does
    3. Yes, also see perlre on what the backslash does
Re: Basic regular expressions
by toolic (Bishop) on Oct 21, 2010 at 13:18 UTC
    In addition to reading the documentation that Corion mentioned, here is one tool that can help decipher regexes: YAPE::Regex::Explain. For example:
    use YAPE::Regex::Explain; print YAPE::Regex::Explain->new('\*')->explain(); The regular expression: (?-imsx:\*) 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): ---------------------------------------------------------------------- \* '*' ---------------------------------------------------------------------- ) end of grouping ----------------------------------------------------------------------
Re: Basic regular expressions
by muba (Priest) on Oct 21, 2010 at 12:55 UTC

    1. This code is just looking for any number of backslashes
    if ($var ~= m/\*/)
    No, the backslash works as an escape character. You're matching for a literal "*".

    2. This code is matching for a backslash but what does the $ mean
    unless($path ~= m/\/$/)
    No. The backslashs works as an escape character again. '$' means 'match at end of string' (you seem to know the meaning of $ in your next question, though). So this matches against a forward slash at the end of $path.

    3. This code is replacing . gz at the end of a string. with nothing
    $output ~= s/\.gz$//;
    Correct.

    You're welcome.

Re: Basic regular expressions
by Ratazong (Monsignor) on Oct 21, 2010 at 13:54 UTC
Re: Basic regular expressions
by raybies (Chaplain) on Oct 21, 2010 at 14:28 UTC

    1. if you want to look for AT LEAST one \ in your code the simplest regex would be...  if ($var =~ /\\/)

    2. this example actually is looking at a forward slash. The dollar ($) symbol is the end of a line or string, so it would be any single slash at the end of a string.

    3. is correct.

    oh and follow the advice of the folks here, and read perlre...