There's more than one way to do things | |
PerlMonks |
Re^3: Regex with Backslashesby AnomalousMonk (Archbishop) |
on May 17, 2020 at 21:03 UTC ( [id://11116874]=note: print w/replies, xml ) | Need Help?? |
The string I was splitting looks like this Both Data::Dumper, which is core, and Data::Dump, which I prefer, but it's not core, represent a string in the form of the double-quote constructor needed to reproduce that string, not as the "actual" string. I think this is one source of your confusion. I think the critical point you're missing is that there is a fundamental difference between a single- or double-quoted string constructor, e.g., '...' or "...", and the string that is constructed. So both '\,' and '\\,' appear the same during processing. No. A string may have one or two or any number of uniquely distinguishable sequential backslashes. The question is how to construct the desired string. Consider In a single-quoted string constructor, \ and \\ are different representations of the same constructed character. This peculiarity of single-quoted string constructors allows a string so constructed (update: to have a single-quote character in a '...'-quoted string, or) to end in a single-quote or backslash character: (Note that in my code examples, I use qq{...} as the double-quote "constructor," as I will call it in this reply, due to peculiarities of the Windoze command line interpreter.) It's possible to (fairly easily) split the double-quoted string you give as an example and get your desired result: The string is split on the pattern "comma that is not preceded by a backslash that is not preceded by a backslash." This sort of tricksy, double-negative logic is part of the reason that a well-tested module like Text::CSV is so often and enthusiastically recommended for this seemingly-simple parsing application. (I hope this module or one like it is what you're referring to when you write about going "the route of a custom parser.") Give a man a fish: <%-{-{-{-<
In Section
Seekers of Perl Wisdom
|
|