Home: Perl Programming Help: Regular Expressions:
Parsing of string



abhijith321
New User

Aug 29, 2010, 8:38 PM


Views: 8765
Parsing of string

Hi I need help

My input is AB1234567DF345CF453

I need output to be print as
AB1234567
DF345
CF453

i.e I need to split the string whenever i get 2 alphabets


I wrote the program using split function

#!/usr/bin/perl

my $data='AB1234567DF345CF453';

my @values = split(/([A-Z][A-Z][1-8]*)/, $data);

chomp(@values);

foreach my $val(@values)
{
print "$val\n";
}

But i getting output as
<New Line>
AB1234567
<New Line>
<New Line>
DF345
<New Line>
<New Line>
CF453
<New Line>

I need the desired output without new lines
AB1234567
DF345
CF453



If i use period in the print state i.e print "val."
the output is
.AB1234567..DF345.CF453

Any help is highly appreciated.


tej.87g
Novice

Aug 30, 2010, 4:17 AM


Views: 8753
Re: [abhijith321] Parsing of string

Check this should work:

Code
#!/usr/bin/perl 


my $data='AB1234567DF345CF453';
my @arr;
while ($data=~m/([A-Z]{2}\d+)/g)
{
push @arr,$1;
}


foreach my $val(@arr)
{
print "$val\n";
}



abhijith321
New User

Aug 30, 2010, 6:33 AM


Views: 8749
Re: [tej.87g] Parsing of string

Thanks ... u saved my day