Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Problem with input strings that have "[]" brackets

by Laszlo (Novice)
on May 08, 2015 at 17:37 UTC ( [id://1126120]=perlquestion: print w/replies, xml ) Need Help??

Laszlo has asked for the wisdom of the Perl Monks concerning the following question:

I have the following code:
my $line = ' [Category("notestrecord")]'; my $pattern = 'Category("notestrecord")'; if($line =~ /$pattern/) { print "We have a match!"; } else { print "No match!"; }
It is not giving me a match. :-( Where it should be...

Replies are listed 'Best First'.
Re: Problem with input strings that have "[]" brackets
by toolic (Bishop) on May 08, 2015 at 17:42 UTC
      Yes it worked!
Re: Problem with input strings that have "[]" brackets
by edimusrex (Monk) on May 08, 2015 at 18:47 UTC
    You can also do the following

    my $line = ' [Category("notestrecord")]'; my $pattern = 'Category("notestrecord")'; if($line =~ /\Q$pattern/) { print "We have a match!"; } else { print "No match!"; }


    By adding the \Q switch before the $pattern is the regex match
Re: Problem with input strings that have "[]" brackets
by Laszlo (Novice) on May 08, 2015 at 17:51 UTC
    Also realized that not the [] are the problem, it is the matching string. The unprotected "()" parenthesis gave a problem! This too would been fine too:
    my $pattern = 'Category\("notestrecord"\)';
    I am tired need to go home. :-) But yes I should use quotemeta, specially because I am reading it from parameters. :-) Thank you!
      my $pattern = 'Category\("notestrecord"\)';
      Yes, sure this works, and that's essentially what quotemeta is doing to your string..

      Je suis Charlie.
Re: Problem with input strings that have "[]" brackets
by golux (Chaplain) on May 09, 2015 at 01:36 UTC
    Please also note that when looking for a pattern contained as a substring of a line, it can be much simpler and clearer to use the index function:
    my $line = ' [Category("notestrecord")]'; my $pattern = 'Category("notestrecord")'; if (index($line, $pattern) >= 0) { print "We have a match!\n"; } else { print "No match!\n"; }
    The index function gives a negative one if the match wasn't found, and the position of the match if it was (which can be a zero if the match was at the beginning), hence the comparison of zero or greater:
    if (index($line, $pattern) >= 0) { ... }
    say  substr+lc crypt(qw $i3 SI$),4,5
Re: Problem with input strings that have "[]" brackets
by ww (Archbishop) on May 08, 2015 at 22:47 UTC

    Using qr (quote regex-wise) and allowing for the spaces in $line also works:

    #!/usr/bin/perl -w use 5.018; # 1126143 my $line = ' [Category("notestrecord")]'; my $pattern = qr/[Category("notestrecord")]/; if($line =~ /\s*$pattern/) { print "Matched |$pattern| in |$line|"; # vbars to show spaces (& + lack of spaces) } else { print "No match!"; } =head OUTPUT: Matched |(?^u:[Category("notestrecord")])| in | [Category("note +strecord")]| =cut

      Using qr (quote regex-wise) and allowing for the spaces in $line also works:

      use 5.018; my $line = ' [Category("notestrecord")]'; my $pattern = qr/[Category("notestrecord")]/; if($line =~ /\s*$pattern/)

      Are sure that qr// in combination with use 5.018 magically adds the missing quotemeta? My perl 5.18.1 does not show that behavior:

      >perl -e 'use 5.018; my $x=qr/a[bc]d/; say("|$_| matches |$x|: ",$_=~/ +$x/ ? "yes" : "no") for ( "abd","acd","abcd","a[bc]d" )' |abd| matches |(?^u:a[bc]d)|: yes |acd| matches |(?^u:a[bc]d)|: yes |abcd| matches |(?^u:a[bc]d)|: no |a[bc]d| matches |(?^u:a[bc]d)|: no >

      Your script matches $line against zero or more whitespace characters, followed by one of the characters "()Cacdegnorsty. The C in $line matches this condition.

      I can freely change the order of characters and remove duplicates in qr/[]/ without changing the script output:

      #!/usr/bin/perl -w use 5.018; # 1126143 my $line = ' [Category("notestrecord")]'; my $pattern = qr/["()Cacdegnorsty]/; if($line =~ /\s*$pattern/) { print "Matched |$pattern| in |$line|"; # vbars to show spaces (& + lack of spaces) } else { print "No match!"; } =head OUTPUT: Matched |(?^u:[Category("notestrecord")])| in | [Category("note +strecord")]| =cut
      >perl 1126159-modified.pl Matched |(?^u:["()Cacdegnorsty])| in | [Category("notestrecord" +)]| >

      Alexander

      --
      Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)

        afoken: I think you're right on all counts, but have not satisfied myself that I understand the implications of the discussion of 'normaliz(ation) in http://perldoc.perl.org/perlop.html#Regexp-Quote-Like-Operators.

        Just BTW (and it seems to make no difference in my experiments, your Line 7 lacks the closing double-quote found in OP's datum... but I've (so far) found no docs explaining why quantification isn't required (could it be related to some aspect of normalization?).

        The text and example make it clear that qr() (except when using

        qr"..." delimiters) normalizes the expression -- re-orders the content +s as in your examples.</p> <p>And my attempts to requote (using <c>\"
        or qq your *nix-ish one-liners have come up (badly!) short... so this is merely an interim response on those.

        Clearly, I have more reading to do (but pointers are welcome)!

        UPDATE: (after TWO cups) Even more clearly, coffee must be kicking in: it finally occured to me to do the nix-win translation in a file (1126143afoken-222.pl) rather than as a one-liner.

        The results match yours:
        |abd| matches |(?^u:a[bc]d)|: yes |acd| matches |(?^u:a[bc]d)|: yes |abcd| matches |(?^u:a[bc]d)|: no |a[bc]d| matches |(?^u:a[bc]d)|: no

        But, informative pointers remain very welcome!


        ++$anecdote ne $data
        ...and that's surely a problem, so far, with this reply.

        check Ln42!

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (5)
As of 2024-04-19 03:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found