This isn't too hard to fix:

my( $quote, $quoted, $end )= $string =~ /(['"])((?:\\.|[^'"\\]+|(?!\1)['"])*)(\1?)/; die "Unclosed quote: $quote$quoted\n" if $quote && ! $end;
You can (not) also use the simpler:
/(['"])((?:\\.|[^\1\\]+)*)(\1?)/
but I suspect that [^\1] didn't work in some slightly older versions of Perl (especially since the original regular expression goes out of its way to avoid it).

If you have a string that contains a huge sequence of backquoted characters, then you might have to add a + to that part of the regex as well:

/(['"])((?:(?:\\.)+|[^\1\\]+)*)(\1?)/
(rather, use this corrected one
/(['"])((?:(?:\\.)+|[^'"\\]+|(?!\1)['"])*)(\1?)/
). Though that still breaks on
"'" . '\vv'x35_000 . "z'"
which would force you to do something more like (updated):
my( $quote, $quoted ); if( $str =~ /(['"])/g ) { my $beg= pos($str); $quote= $1; if( $str !~ /(?<!\\)((?:\\\\)*)\Q$quote/g ) { die "Unclosed quote: ", substr($str,$beg), $/; } my $end= pos($str); $quoted= substr( $str, $beg, $end-$beg-1 ); }
(:

Update: Thanks, merlyn. I knew that had failed in my previous testing but had also run into people thinking it should work enough times that when it "worked" in my test case that didn't test that part of it at all, I jumped to the wrong conclusion.

                - tye

In reply to Re: Text::ParseWords regex doesn't work when text is too long? (fixes) by tye
in thread Text::ParseWords regex doesn't work when text is too long? by edan

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.