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

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

by afoken (Chancellor)
on May 09, 2015 at 13:07 UTC ( #1126190=note: print w/replies, xml ) Need Help??

in reply to Re^3: Problem with input strings that have "[]" brackets
in thread Problem with input strings that have "[]" brackets

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

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.


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

Log In?

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

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (5)
As of 2023-12-08 19:32 GMT
Find Nodes?
    Voting Booth?
    What's your preferred 'use VERSION' for new CPAN modules in 2023?

    Results (37 votes). Check out past polls.