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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |