Using existing modules is always the best solution, because they handle complex cases and have had more error testing than you will invest for a few lines of code.

split /(")/ will return an array where each element is either unquoted text, a quote mark, or text froma quotation. If the first element is a quote, the array starts with a quotation, otherwise it starts with unquoted text. Check the last character of each string for a backslash, to determine whether the quotation mark is a real quote, or has been escaped. But if you find a backslash, make sure there is an odd number of backslashes; an even number simply puts literal backslashes into the string, but does not modify the quote. So it isn't simply, "odd block, unquoted; even block, quoted", but rather a block by block state machine.

Of course, if you allow both single quotes and double quotes to delimit strings, things become much more complicated. You could split on /('")/, but processing is stressfull. If you see a single quote within a double quote, is it an inner quote or a contraction/possessive?

John said, "Don't do it!".

Mary said, "I told you, 'Stop!'".

Paul said, "Stephen's watch is missing."

Assuming grammatically correct text, a single single quote within quoted text is a contraction or possessive. but if there are more than one, does that indicate an inner string, or multiple constractions and possessives?

I've seen many cases where people thought it too expensive to load a module just to handle some simple situation, like detecting balanced strings, or doing simple date calculations, when all it needs is a couple lines of code. This works fine until months or years later, when one of your data feeds starts sending data which includes that impossible special case your code doesn't handle. After hours or days of debugging, some poor sucker maintenance programmer me curses you for taking the easy wrong solution.

--
TTTATCGGTCGTTATATAGATGTTTGCA


In reply to Re: need to recognize string literals and ignore then during parsing. by TomDLux
in thread need to recognize string literals and ignore then during parsing. by Elijah

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.