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

Hi monks,

I need help understanding what qr does.

I have this expression qr/^data_123$/i and I want it match the string exactly, but it does not seem to work. When I tried to print the expression, I see that it is getting compiled into ?^i:^data_123$. I dont understand why, "?^i" part got added, all I want is to match data_123 to match exactly. How do I do this.?

Replies are listed 'Best First'.
Re: Compiled regular expression using qr
by GrandFather (Saint) on Oct 15, 2014 at 20:28 UTC

    Show us test code with data that shows the problem. Most likely you are reading the string from STDIN and haven't chomped it.

    Perl is the programming world's equivalent of English
      The comaprison is done in a framework subroutine, but it goes like this
      my ($fsObj) = $HostA->find( type => 'DS', criteria => { name =>qr/^data_123$/i}, force_sync =>1, );

      and in the frmeowrk routine

      sub find { foreach my $k (keys %{$params{criteria}}) { my $objVal=$_[0]->getProperty($k);; my $critVal = $params{criteria}{$k}; if (defined $objVal) { return 0 if (!($self->_compareCriteria($objVal +,$critVal))); } } sub _compareCriteria { my ($self) = shift(@_); my ($objectVal, $critVal) = (@_); if (ref($critVal) eq 'Regexp') { return ($objectVal =~ $critVal) ? 1 : 0; }

        So if I make small changes to allow the code to be run in a test harness I get:

        use strict; use warnings; my $HostA = bless {name => 'data_123'}; my ($fsObj) = $HostA->find( type => 'DS', criteria => {name => qr/^data_123$/i}, force_sync => 1, ); print $fsObj || 'not matched'; sub find { my ($self, %params) = @_; foreach my $k (keys %{$params{criteria}}) { my $objVal = $self->getProperty($k); my $critVal = $params{criteria}{$k}; if (defined $objVal) { return 0 if (!($self->_compareCriteria($objVal, $critVal)) +); } } return 'All matched'; } sub _compareCriteria { my ($self) = shift(@_); my ($objectVal, $critVal) = (@_); if (ref($critVal) eq 'Regexp') { return ($objectVal =~ $critVal) ? 1 : 0; } } sub getProperty { my ($self, $prop) = @_; return $self->{$prop}; }

        which prints:

        All matched

        Maybe you'd like to modify that to show the problem you see?

        Update: as an aside, this doesn't look like new code written by someone with an apparently limited understanding of regular expressions. Is this maintenance work by someone thrown in at the deep end perhaps?

        Perl is the programming world's equivalent of English
Re: Compiled regular expression using qr
by LanX (Saint) on Oct 15, 2014 at 20:29 UTC
    > but it does not seem to work.

    Why? Can you show us code?

    > dont understand why, "?^i" part got added

    Cause you appended /i which means case insensitive.

    The way a compiled regex is stringified changed between Perl versions, but that doesn't mean they work differently now.

    Cheers Rolf

    (addicted to the Perl Programming Language and ☆☆☆☆ :)

      Thanks!,

      I tired printing both origVal and critVal

      data_123 and (?^:data_123)

      Apparently data_123 != ?^:data_123 I removed case insensitive in my query string.

        In the OP and in your reply above, you use the terms "exact" and "case insensitive" in the context of regex matching in a way that leads me to wonder if you understand that case insensitive matching is inherently inexact. E.g.:

        c:\@Work\Perl>perl -wMstrict -le "my $rx = qr/^data_123$/i; print $rx; ;; for my $s (qw(data_123 DATA_123 dAtA_123 data_123x data_12 nada_123) +) { if ($s =~ $rx) { print qq{'exact' match to '$s'}; } else { print qq{NO match to '$s'}; } } " (?^i:^data_123$) 'exact' match to 'data_123' 'exact' match to 'DATA_123' 'exact' match to 'dAtA_123' NO match to 'data_123x' NO match to 'data_12' NO match to 'nada_123'

        Update: I should have mentioned that the example above was run under Strawberry Perl version 5.14.4.1.