Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re^6: Parser Performance Question (updated)

by Eily (Monsignor)
on Oct 06, 2017 at 08:02 UTC ( [id://1200800]=note: print w/replies, xml ) Need Help??


in reply to Re^5: Parser Performance Question (updated)
in thread Parser Performance Question

Meh, this will teach me about posting when I can't test. I meant q< \\\\" >; but thought the \ wouldn't escape the next one. The case where the \ just before the " is litteral because escaped, so it can't escape the ".

use v5.20; use Data::Dump qw( pp ); my $str = <<STR; " .. \\\\" .. " STR print $str; my %re = (LanX => qr/ " (?: \\\\ | \\" | [^"] )* " /x, Eily => qr/ " (?: [^\\] | \\. )* " /x); $str =~ /$re{$_}/ and say "$_ found $&" for keys %re;
" .. \\" .. " Eily found " .. \\" .. " LanX found " .. \\"

Replies are listed 'Best First'.
Re^7: Parser Performance Question (updated)
by LanX (Saint) on Oct 06, 2017 at 08:37 UTC
    >
    " .. \\" .. " Eily found " .. \\" .. " LanX found " .. \\"

    Well, you proved that my version is correct and yours doesn't work.

    :p

    Cheers Rolf
    (addicted to the Perl Programming Language and ☆☆☆☆ :)
    Je suis Charlie!

      Hu, how did I ever reach sainthood in our book? Eily => qr/ " (?: [^"\\] | \\. )* " /x); <= added the missing " in the character class

      use Data::Dump qw( pp ); my $str = <<STR; " .. \\" .. STR print $str; my %re = (LanX => qr/ " (?: \\\\ | \\" | [^"] )* " /x, Eily => qr/ " (?: [^"\\] | \\. )* " /x); $str =~ /$re{$_}/ and say "$_ found $&" or say "$_ found nothing" for +keys %re;
      " .. \" .. Eily found nothing LanX found " .. \"

        Atomic grouping (?>...) seems to fix the problem
        use strict; use warnings; use feature 'say'; use Data::Dump qw( pp ); my @strs = qw( "..\\".. "abc" "a\"bc" "a\\\\bc" "a\" ); my %re = ( LanX => qr/ " (?> \\\\ | \\" | [^"] )* " /x, Eily => qr/ " (?: [^"\\] | \\. )* " /x ); for my $str (@strs) { say "\nTesting: <$str> = ", pp ($str); $str =~ /$re{$_}/ and say "$_ found $&" or say "$_ found nothing" for keys %re; }

        Testing: <"..\"..> = "\"..\\\".." LanX found nothing Eily found nothing Testing: <"abc"> = "\"abc\"" LanX found "abc" Eily found "abc" Testing: <"a\"bc"> = "\"a\\\"bc\"" LanX found "a\"bc" Eily found "a\"bc" Testing: <"a\\bc"> = "\"a\\\\bc\"" LanX found "a\\bc" Eily found "a\\bc" Testing: <"a\"> = "\"a\\\"" LanX found nothing Eily found nothing

        Cheers Rolf
        (addicted to the Perl Programming Language and ☆☆☆☆ :)
        Je suis Charlie!

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (6)
As of 2024-04-24 08:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found