Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re^2: Use of uninitialized value $1

by Pazitiff (Novice)
on Aug 25, 2016 at 12:27 UTC ( [id://1170416]=note: print w/replies, xml ) Need Help??


in reply to Re: Use of uninitialized value $1
in thread Use of uninitialized value $1

Thanks! But what if i want capture the left word ("path_to_my_") of "lib"? In general, I need a regular expression to get what is left of the "lib". How this can be done using $1, $2, $3 ? Thanks. Sorry, but i have no experience in Perl and RX.

Replies are listed 'Best First'.
Re^3: Use of uninitialized value $1
by AnomalousMonk (Archbishop) on Aug 25, 2016 at 12:41 UTC

    The Devil is in the Details! Exactly what are you matching against, and exactly what do you want to match?

    c:\@Work\Perl\monks>perl -wMstrict -le "my $var = '%&!~ path_to_a_lib_of_my_lib'; ;; $var =~ m{ (.*) lib }xms; print qq{A: '$1'}; ;; $var =~ m{ (.*?) lib }xms; print qq{B: '$1'}; ;; $var =~ m{ (\w*?) lib }xms; print qq{C: '$1'}; " A: '%&!~ path_to_a_lib_of_my_' B: '%&!~ path_to_a_' C: 'path_to_a_'

    Update: Please see perlre, perlretut, and perlrequick.

    Further Update: If the string consists entirely in something like 'path_to_a_lib_of_my_lib', see also the start- and end-of-string "anchors"  \A \z \Z (particularly  \A \z in this case) for further narrowing what you want to capture.


    Give a man a fish:  <%-{-{-{-<

Re^3: Use of uninitialized value $1
by FreeBeerReekingMonk (Deacon) on Aug 25, 2016 at 12:52 UTC
    my $v = '/path/to/module.pm'; $v =~ m{^(.*)/(.*)$}; say "($1) ($2)";

    prints out:

    (/path/to) (module.pm)

    Thus, if you want your module name hardcoded:

    my $modulepath; if($v =~ m{^(.*)module.pm$}){ $modulepath = $1; }

    additional note: usually you write m// with slashes, but I write it with m{} brackets. This is also valid and allows you to use / inside the regexp, without having to escape it with a backslash.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1170416]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (6)
As of 2024-04-18 21:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found