in reply to Improved regexp sought
#!/usr/local/bin/perl -w use strict; $/="\'\n"; #set the IFS while (<DATA>) { chomp; # chomp strips the IFS s/\?\'/\'/g; # fix the "quoted" ' marks my @fields=split /\+/; print "[$_] $fields[$_]\n" for 0..$#fields; } __DATA__ 0010+2+O'Reilly' 023++++234+35+White+++17+' g?'day mate+++' # output [0] 0010 [1] 2 [2] O'Reilly [0] 023 [1] [2] [3] [4] 234 [5] 35 [6] White [7] [8] [9] 17 [0] g'day mate
possibly a bit more elegant for certain values of elegance
$/="\'\n"; while (<DATA>) { chomp; s/\?\'/\'/g; my $i; print "[",$i++,"] $_\n" for split /\+/; }
I keep getting voted down for this node so I think I had better explain myself. I am not doing it all in regex as the OP wanted to do but the OP says the data is in a file one record per line terminated with a '. As he has to read the line in anyway, and we can guess from the example string given in the lead post that he is also chomping the line he reads why not use the IFS to solve the terminal ' issue efficiently. Once you have reached this point the fix "?'" and split is surely more efficient and maintainable than some confusing regex.
Cheers,
R.
|
|---|