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

Re: Problem with input strings that have "[]" brackets

by ww (Archbishop)
on May 08, 2015 at 22:47 UTC ( [id://1126159]=note: print w/replies, xml ) Need Help??


in reply to Problem with input strings that have "[]" brackets

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

Replies are listed 'Best First'.
Re^2: Problem with input strings that have "[]" brackets
by afoken (Chancellor) on May 09, 2015 at 06:19 UTC

    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!

        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...

        No. Everything inside unquoted [] inside qr// is a character class. Repeating characters inside [] is allowed, but has no effect. Double-quotes have no special meaning there, they are just like almost any other character. In fact, they are the first character only because I sorted all of the characters found in your $pattern line by their ASCII code:

        >perl -E '$x=q[Category("notestrecord")];@a=split "",$x;say join "" => + sort grep { !$seen{$_}++ } @a' "()Cacdegnorsty >

        You could move the double quotes to any other position between [] without any effect. See Bracketed Character Classes.

        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.

        Should not be very relevant here. The most important part (perhaps the only one?) is that RE modifiers are merged into the RE using embedded pattern-match modifiers ((?:pattern), (?adluimsx-imsx:pattern), or (?^aluimsx:pattern) notation):

        >perl -e 'print "$_\n" for (qr/ab.c/,qr/ab.c/i,qr/ab.c/s,qr/ab.c/si)' (?^:ab.c) (?^i:ab.c) (?^s:ab.c) (?^si:ab.c) >

        Enabling newer perl features may add the unicode flag:

        >perl -E 'say for (qr/ab.c/,qr/ab.c/i,qr/ab.c/s,qr/ab.c/si)' (?^u:ab.c) (?^ui:ab.c) (?^us:ab.c) (?^usi:ab.c) >

        See Extended Patterns.

        Normalizing does not clean up redundant characters in character classes, nor does it sort characters in character classes - at least not in my perl 5.18.1. Enabling features does not change that. Using qr"" instead of qr// also has no effect:

        >perl -e 'print "$_\n" for (qr/[abc]/,qr/[aabbcc]/,qr/[abcabc]/,qr/[cc +cbba]/)' (?^:[abc]) (?^:[aabbcc]) (?^:[abcabc]) (?^:[cccbba]) >perl -E 'say for (qr/[abc]/,qr/[aabbcc]/,qr/[abcabc]/,qr/[cccbba]/)' (?^u:[abc]) (?^u:[aabbcc]) (?^u:[abcabc]) (?^u:[cccbba]) >perl -e 'print "$_\n" for (qr"[abc]",qr"[aabbcc]",qr"[abcabc]",qr"[cc +cbba]")' (?^:[abc]) (?^:[aabbcc]) (?^:[abcabc]) (?^:[cccbba]) >

        (Note that ALL of the test patterns match a single character a, b, or c).

        There is a difference between $pattern="pattern"; $foo=~/$pattern/, $pattern=qr/pattern/; $foo=~/$pattern/, and $pattern=qr/pattern/; $foo=~$pattern: The first example is simple string interpolation, $pattern is just a simple scalar that will be compiled every time it is used in a regexp. The second and third example creates a Regexp object with a (possibly) compiled regular expression, using it in a regexp avoids recompiling it.

        So, qr is not just a quoting operator for regular expressions (despite its name), but a constructor for Regexp (object) references. Those objects stringify to an equivalent (but not necessarily equal) regexp string. Perl may choose to compile the pattern inside the Regexp (object) reference. The exact wording in perlop is "This operator quotes (and possibly compiles) its STRING as a regular expression." in 5.20.1, "A string which is (possibly) interpolated and then compiled as a regular expression." in 5.005.

        Alexander

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

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (1)
As of 2024-04-18 23:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found