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

Re: regex for strings with escaped quotes

by haukex (Bishop)
on Feb 12, 2019 at 15:13 UTC ( #1229801=note: print w/replies, xml ) Need Help??


in reply to regex for strings with escaped quotes

use Regexp::Common qw/delimited/; my $str = q{ x "foo \"bar\"" y }; $str =~ /($RE{delimited}{-delim=>'"'})/; print $1, "\n"; print $RE{delimited}{-delim=>'"'}, "\n"; __END__ "foo \"bar\"" (?:(?|(?:\")(?:[^\\\"]*(?:\\.[^\\\"]*)*)(?:\")))

Replies are listed 'Best First'.
Re^2: regex for strings with escaped quotes
by morgon (Priest) on Feb 12, 2019 at 15:52 UTC
    It somehow stops working when I use the regex directly and I cannot see why:
    my $str = q{ x "foo \"bar\"" y }; $str =~ /(?:(?|(?:\")(?:[^\\\"]*(?:\\.[^\\\"]*)*)(?:\")))/; print $1, "\n"; # prints nothing
      $str =~ /(?:(?|(?:\")(?:[^\\\"]*(?:\\.[^\\\"]*)*)(?:\")))/;

      It's missing the capture group that I added: /($RE{delimited}{-delim=>'"'})/

        I don't quite understand...

        I do basically this:

        use Regexp::Common qw/delimited/; print $RE{delimited}{-delim=>'"'};
        And take what is printed.

        How can I get to a pure regex that works then?

Re^2: regex for strings with escaped quotes
by morgon (Priest) on Feb 12, 2019 at 15:17 UTC
    thanks, but I forgot to mention one detail:

    I do not need this for a perl-program but for a perl5-compatible regex engine in Go.

    I there a way to print the regex that is used?

      I there a way to print the regex that is used?

      That's what the second line of output above is. Simplification of that regex is left as an exercise to the reader :-) (Update: Nevermind.)

      BTW, you can also use the -keep feature to get only the part between the quotes:

      use Regexp::Common qw/delimited/; q{ x "foo \"bar\"" y } =~ /$RE{delimited}{-delim=>'"'}{-keep}/; print $3, "\n"; # prints: foo \"bar\"
        Many thanks.

        This is exactly what I need - let's see if I can feed that to the go-engine...

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (3)
As of 2021-03-05 00:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My favorite kind of desktop background is:











    Results (108 votes). Check out past polls.

    Notices?