Paul Paul - 2 months ago 13
Perl Question

perl regex square brackets and single quotes

Have this string:

ABC,-0.5,10Y,10Y,['TEST'],ABC.1000145721ABC,-0.5,20Y,10Y,['TEST'],ABC.1000145722


The data is repeated.

I need to remove the []' characters from the data so it looks like this:

ABC,-0.5,10Y,10Y,TEST,ABC.1000145721ABC,-0.5,20Y,10Y,TEST,ABC.1000145722


I'm also trying to split the data to assign it to variables as seen below:

my($currency, $strike, $tenor, $tenor2,$ado_symbol) = split /,/, $_;


This works for everything but the ['TEST'] section. Should I remove the []' characters first then keep my split the same or is there an easier way to do this?

Thanks

Answer

Something that's useful to know is this - that split takes a regex. (It'll even let you capture, but that'll insert into the returned list, which is why I've got (?: for non capturing groups)

I observe your data only has [' right next to the delimiter - so how about:

#!/usr/bin/env perl
use strict;
use warnings;
use Data::Dumper;

while ( <DATA> ) {
  chomp;
  my @fields = split /(?:\'])?,(?:\[\')?/; 
  print Dumper \@fields;
}

__DATA__
ABC,-0.5,10Y,10Y,['TEST'],ABC.1000145721ABC,-0.5,20Y,10Y,['TEST'],ABC.1000145722

Output:

$VAR1 = [
          'ABC',
          '-0.5',
          '10Y',
          '10Y',
          'TEST',
          'ABC.1000145721ABC',
          '-0.5',
          '20Y',
          '10Y',
          'TEST',
          'ABC.1000145722'
        ];