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

Fellow Monks,

I am trying to match quoted strings in a piece of perl code like this:
$string = q[Jon 'Maddog' Orwant]; $string = q{Jon 'Maddog' Orwant}; $string = q(Jon 'Maddog' Orwant); $string = q<Jon 'Maddog' Orwant>;

I could do it the dumb way:
m/(q[qxwr]?)(\[.*?\]|\{.*?\}|\(.*?\)|\<.*?\>)/
But it's so ugly. Would anybody have a cleaner solution?

Replies are listed 'Best First'.
Re: [regex] matching Perl quoted strings
by Abigail-II (Bishop) on Mar 16, 2004 at 12:38 UTC
    It ain't simple. Your 'dumb' way isn't correct. Consider that the following are valid Perl strings too:
    q [Jon [Maddog] Orwant]; q "Jon \"Maddog\" Orwant";
    Your regex doesn't match any of them correctly.

    You might be best off using Text::Balanced.

    Abigail

      Hmmmzzz good point.
      The problem is that i really want to do it with a single regex because of the current flow of the program.
        The problem is that I really want to do it with a single regex because of the current flow of the program.

        Then rework the current flow of the program. A single regex is not the tool you need. Text::Balanced is. Have you read the docs for Text::Balanced yet? It may be that you don't have to redesign your program all that much to use it.

        Perhaps you could maintain the current flow if you wrap the dirty-work done by Text::Balanced in a subroutine.


        Dave

        Well, I'm not going to claim it's impossible to do with a regexp. It certainly is. But I don't have time to work out the details right now, and, considering there's a module doing what you want to do, I don't have any initiative to spend time on it.

        Abigail

Re: [regex] matching Perl quoted strings
by chip (Curate) on Mar 16, 2004 at 15:09 UTC
    I recommend Text::Balanced. "If it's good enough for Damnian, it's likely to kill you. But it'll be fun."

        -- Chip Salzenberg, Free-Floating Agent of Chaos

Re: [regex] matching Perl quoted strings
by zby (Vicar) on Mar 16, 2004 at 12:41 UTC
    The Regexp::Common::balanced module should be applicable here. I've never used it so I can't comment on it's quality.

    Update: Or rather I can comment on its quality seeing that the author of that piece of sofware is Damian Conway.

      The Regexp::Common::balanced module should be applicable here.
      Uhm, no. Perl allows delimiters to be escaped, while Regexp::Common::balanced doesn't consider escapes. (But as of now, it's on the TODO list).

      Abigail

Re: [regex] matching Perl quoted strings
by dawn (Novice) on Mar 16, 2004 at 15:03 UTC
    Instead of the .*? which will take up more processing, you could use negated characters

    e.g.
    /(\[[^]]*\] | {[^}]*} | \([^)]*\) | <[^>]*>)/