Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical

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.

Replies are listed 'Best First'.
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


    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 (Prior) 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 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 Lennotoecom (Pilgrim) on Dec 05, 2013 at 00:04 UTC
    you can match it with eq
    if regexp doesn't work

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1065634]
Approved by herveus
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (7)
As of 2018-06-20 23:15 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (117 votes). Check out past polls.