upaksh has asked for the wisdom of the Perl Monks concerning the following question:

hello

I have following string from which the resultant string i need as

((113000.00 Nos.) + (250000.00 Nos.) ++ (1) )

I need something like

 113000.00 + 250000.00 + 1 Thanks...

Replies are listed 'Best First'.
Re: regexp help
by Anonymous Monk on Dec 15, 2011 at 12:37 UTC

    so what have you tried ?

    #!/usr/bin/perl -- use strict; use warnings; my $input = '((113000.00 Nos.) + (250000.00 Nos.) ++ (1) )'; my $output = ' 113000.00 + 250000.00 + 1 '; print "$input\n"; $input =~ s/ No\S+ //gx; #~ $input =~ tr/ +//s; # squash repeating space and + characters #~ $input =~ tr/)(//d; # delete ) and ( $input =~ tr/+ )(/+ /s; # squash repeating space and +, delete ) and ( print "$output\n"; print "$input\n"; __END__ ((113000.00 Nos.) + (250000.00 Nos.) ++ (1) ) 113000.00 + 250000.00 + 1 113000.00 + 250000.00 + 1
Re: regexp help
by AnomalousMonk (Archbishop) on Dec 15, 2011 at 21:07 UTC

    Also with the caveat that the true variation in the data is unknown, here's another approach:

    >perl -wMstrict -le "use Regexp::Common; ;; my $s = '((113000.00 Nos.) + (250000.00 Nos.) ++ (1) )'; ;; my $extract = join ' ', grep defined, $s =~ m{ ($RE{num}{real}) | ([+])+ }xmsg ; print qq{'$extract'}; " '113000.00 + 250000.00 + 1'

    (And see also perlretut and perlrequick.)

Re: regexp help
by si_lence (Deacon) on Dec 15, 2011 at 14:20 UTC
    I am not sure what the variation in your data can be. For the example given, the following should to the trick.
    my $input = '((113000.00 Nos.) + (250000.00 Nos.) ++ (1) )'; $input =~ s/\(|\)| Nos\.//g; $input =~ s/\+\+/+/g;
    That said, in the long run it probaly will help you to get familiar with regular expressions by reading something like perlre
Re: regexp help
by upaksh (Novice) on Dec 16, 2011 at 05:31 UTC
    Thanks guys.. I also found the solution myself... $str = ((113000.00 Nos.) + (250000.00 Nos.) ++ (1) ) $str =~ s/[a-zA-Z()]\.*//g; $str =~ s/\++/\+/g;

    this solves the problem for me and gives the required result