Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Help with a regex

by cspctec (Sexton)
on Dec 04, 2013 at 19:07 UTC ( #1065634=perlquestion: print w/ replies, xml ) Need Help??
cspctec has asked for the wisdom of the Perl Monks concerning the following question:

I have an array that has the following values:
nfcclib lib
and I need to match only "lib", not "nfcclib". I have tried the following regex with no success...
/^lib$/ /^lib/
I thought the carrot and dollar sign meant the beginning and end of the string, but it matches nfcclib also. Any help is appreciated.

Comment on Help with a regex
Select or Download Code
Re: Help with a regex
by monkey_boy (Priest) on Dec 04, 2013 at 19:22 UTC
    show your code, as the carrot and dollar solution should work, perhaps the bug is elsewhere or there are white-spaces in your array values.



    This is not a Signature...
Re: Help with a regex
by Kenosis (Priest) on Dec 04, 2013 at 19:22 UTC

    I get the following:

    use warnings; use strict; use Data::Dumper; chomp( my @array = <DATA> ); for (@array) { chomp; if (/^lib$/) { print "Yes: $_\n"; } else { print "No : $_\n"; } } print "\n", Dumper \@array; __DATA__ nfcclib lib

    Output:

    No : nfcclib Yes: lib $VAR1 = [ 'nfcclib', 'lib' ];

    Consider using Data::Dumper to examine the array's elements.

    Hope this helps

    Edit: Added Data::Dumper.

Re: Help with a regex
by Eily (Curate) on Dec 04, 2013 at 19:24 UTC

    You understood ^ and $ correctly, you probably test it wrong. Could you give us the code sample that makes you reach that conclusion ?

    perl -E 'say /^lib/ ? "$_ matched" : "$_ did not match" for qw/nfcclib lib/;'

    nfcclib did not match lib matched

    Alternatively, you could also use \b which means word boundary (so the regex m<\blib>; or its equivalent /\blib/)

Re: Help with a regex
by Lennotoecom (Pilgrim) on Dec 05, 2013 at 00:04 UTC
    you can match it with eq
    if regexp doesn't work

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (8)
As of 2015-07-04 15:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (60 votes), past polls