CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

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

 



abhijith321
New User

Aug 29, 2010, 8:38 PM

Post #1 of 3 (3589 views)
Parsing of string Can't Post

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

Post #2 of 3 (3577 views)
Re: [abhijith321] Parsing of string [In reply to] Can't Post

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

Post #3 of 3 (3573 views)
Re: [tej.87g] Parsing of string [In reply to] Can't Post

Thanks ... u saved my day

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives