http://qs1969.pair.com?node_id=199438

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

Need help figuring this out:
.*${name}-*
It is saying any letters and then a literal and then ...?
I am curious to know what this regular expression is doing? Am I on the right track??

Replies are listed 'Best First'.
Re: Regular Expression mystery
by PodMaster (Abbot) on Sep 20, 2002 at 11:15 UTC
    Don't ever guess again.
    use YAPE::Regex::Explain; die YAPE::Regex::Explain->new(qr/.*${name}-*/)->explain; __END__ 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): ---------------------------------------------------------------------- .* any character except \n (0 or more times (matching the most amount possible)) ---------------------------------------------------------------------- -* '-' (0 or more times (matching the most amount possible)) ---------------------------------------------------------------------- ) end of grouping ----------------------------------------------------------------------
    update:
    In case you were wondering where the (?-imsx:.*-*) comes from, well perl -e"print qr/asdf/", perl -e"print ref qr/a/" and since ${name} is not defined anywhere in the above snippet, that part don't show up in the explanation. Here's an alternative
    use YAPE::Regex::Explain; my $name = 'asdf'; die YAPE::Regex::Explain->new(qr/.*${name}-*/)->explain; __END__ The regular expression: (?-imsx:.*asdf-*) 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): ---------------------------------------------------------------------- .* any character except \n (0 or more times (matching the most amount possible)) ---------------------------------------------------------------------- asdf 'asdf' ---------------------------------------------------------------------- -* '-' (0 or more times (matching the most amount possible)) ---------------------------------------------------------------------- ) end of grouping ----------------------------------------------------------------------

    ____________________________________________________
    ** The Third rule of perl club is a statement of fact: pod is sexy.

      I got alot of answers here. Thanks so much to all of you!
Re: Regular Expression mystery
by davorg (Chancellor) on Sep 20, 2002 at 11:13 UTC
    • .* - zero or more of any character
    • ${name} - the contents of $name
    • -* - zero or more '-' characters.
    --
    <http://www.dave.org.uk>

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

Re: Regular Expression mystery
by thinker (Parson) on Sep 20, 2002 at 11:18 UTC
    Hi

    Imagine the variable $name contained "foo", the regex will expand to .*foo-* , which will be true if the line contains any character, followed somewhwere with foo, and one or more - signs.

    cheers

    thinker

    update As can be seen in the replies from the less careless monks who posted before me, the - sign need not appear. apologies.