Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Regular Expression mystery

by Anonymous Monk
on Sep 20, 2002 at 11:08 UTC ( #199438=perlquestion: print w/ replies, xml ) Need Help??
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??

Comment on Regular Expression mystery
Download Code
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 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 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.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://199438]
Approved by RMGir
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (9)
As of 2014-07-28 21:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (209 votes), past polls