in reply to Re^2: Regex with Backslashes (updated)
in thread Regex with Backslashes
If you have control over the format the string is generated in, then why not use a well-established format like CSV? The defaults of Text::CSV are that fields are separated by commas, if a field contains commas (or whitespace), it is surrounded by double quotes, and if a double quote needs to be escaped, then it is doubled up. For example:
use warnings; use strict; use Text::CSV; my $data = <<'END'; 1,"Text,with,commas and ""quotes""",X,99 END open my $fh, '<', \$data or die $!; my $csv = Text::CSV->new({ binary=>1, auto_diag=>2 }); while ( my $row = $csv->getline($fh) ) { print "<<$_>>\n" for @$row; } $csv->eof or $csv->error_diag; close $fh; __END__ <<1>> <<Text,with,commas and "quotes">> <<X>> <<99>>
Maybe I will take the plunge and post my 'lcd daemon with battery meter script' once it is completed.
Yes, that'd be interesting!
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^4: Regex with Backslashes
by anita2R (Scribe) on May 18, 2020 at 20:20 UTC | |
by haukex (Archbishop) on May 18, 2020 at 20:29 UTC | |
by anita2R (Scribe) on May 18, 2020 at 21:04 UTC |