PRyanRay has asked for the wisdom of the Perl Monks concerning the following question:
However, I want to be able to do this for a file that I read in (*.csv). For example, if I use the follwing to read the same file into $_, it does not work:use strict; $_=<<'_quote_'; hai xtest "aa xx aax" baix "xx" x "axa\"x\\" xa "x\\\ \\"x" ax xbai!x _quote_ print "Original:\n", $_, "\n"; s/ ( (?: # at the beginning of the string match till inside the quotes ^(?&outside_quote) " # or continue from last match which always stops inside quotes | (?!^)\G ) (?&inside_quote) # eat things up till we find what we want ) \r?\n # the thing we want to replace ( (?&inside_quote) # eat more possibly till end of quote # if going out of quote make sure the match stops inside them # or at the end of string (?: " (?&outside_quote) (?:"|\z) )? ) (?(DEFINE) (?<outside_quote> [^"]*+ ) # just eat everything till quoting star +ts (?<inside_quote> (?:[^"\\\r\n]++|\\.)*+ ) # handle escapes ) /$1\n$2/xg; print "Replaced:\n", $_, "\n";
Any ideas? And, no I am not able to use the Perl packages Spreadsheet::**** Here, testdata.csv is this business: hai xtest "aa xx aax" baix "xx" x "axa\"x\\" xa "x\\\ \\"x" ax xbai!xmy $file="testdata.csv"; open(FILE, $file) or die "Can't open $file: $!\n"; select((select(FILE), $/ = undef)[0]); $_=<FILE>
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Regular Expressions - replacing newlines and carriage returns inside quotes
by mbethke (Hermit) on Oct 31, 2012 at 15:45 UTC | |
|
Re: Regular Expressions - replacing newlines and carriage returns inside quotes
by 2teez (Vicar) on Oct 31, 2012 at 15:41 UTC | |
by PRyanRay (Novice) on Oct 31, 2012 at 17:47 UTC | |
|
Re: Regular Expressions - replacing newlines and carriage returns inside quotes
by bitingduck (Deacon) on Oct 31, 2012 at 15:42 UTC |