Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Help with a regex

by cspctec (Sexton)
on Dec 04, 2013 at 19:07 UTC ( [id://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

    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 (Monsignor) 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?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
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?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (4)
As of 2024-04-24 11:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found