Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re: Regex for multiple numbers between double quotes

by Athanasius (Archbishop)
on Oct 11, 2016 at 14:18 UTC ( [id://1173731]=note: print w/replies, xml ) Need Help??


in reply to Regex for multiple numbers between double quotes

Hello Akatsuki, and welcome to the Monastery!

It looks like your data may be in CSV (comma-separated values) format. If so, you will be better off (in the long run) to parse it with a dedicated module rather than a regular expression. For example, the following code uses Text::CSV_XS to read the values, then it doubles those which are decimal numbers but leaves the others unchanged:

use strict; use warnings; use Text::CSV_XS; my $csv = Text::CSV_XS->new({ allow_whitespace => 1 }); my $string = '"", "2.90", "3.00", "3.10", "3.20", "3.30", "3.40", "3.5 +0", ' . '"3.60", "3.70", "3.80", "3.90", "4.00", "4.10", "4.20", "5v" +'; if ($csv->parse($string)) { my @fields = $csv->fields; /(\d+\.\d+)/ && ($_ *= 2) for @fields; print "$_\t" for @fields; } else { $csv->error_diag(); }

Output:

0:12 >perl 1707_SoPW.pl 5.8 6 6.2 6.4 6.6 6.8 7 7.2 + 7.4 7.6 7.8 8 8.2 8.4 5v 0:12 >

Hope that helps,

Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

Replies are listed 'Best First'.
Re^2: Regex for multiple numbers between double quotes
by Akatsuki (Novice) on Oct 12, 2016 at 07:15 UTC

    Thank you for your answer, but I think I have expressed myself wrong so I will post my entire code If you can help me. I want to open a file, read the values between the double quotes and replace some numbers from this file with their corresponding value from the first line. This is my code:

    #!/usr/bin/perl # matchtest2.plx use warnings; use strict; open (my $original, '<', 'data.txt') or die $!; open (my $fh, '>', 'output.txt') or die $!; while (my $line = <$original>) { if ($line =~ m/\"(.*?)\", \"(.*?)\", \"(.*?)\", \"(.*?)\", \"(.*?)\", +\"(.*?)\", \"(.*?)\", \"(.*?)\", \"(.*?)\", \"(.*?)\", \"(.*?)\", \"( +.*?)\", \"(.*?)\", \"(.*?)\", \"(.*?)\", \"(.*?)\"/) { my $replace = $1; $line =~ s/[^.]00/$replace/g; } if ($line =~ m/\"(.*?)\", \"(.*?)\", \"(.*?)\", \"(.*?)\", \"(.*?)\", +\"(.*?)\", \"(.*?)\", \"(.*?)\", \"(.*?)\", \"(.*?)\", \"(.*?)\", \"( +.*?)\", \"(.*?)\", \"(.*?)\", \"(.*?)\", \"(.*?)\"/g) { my $replace1 = $2; $line =~ s/01/$replace1/g; } if ($line =~ m/\"(.*?)\", \"(.*?)\", \"(.*?)\", \"(.*?)\", \"(.*?)\", +\"(.*?)\", \"(.*?)\", \"(.*?)\", \"(.*?)\", \"(.*?)\", \"(.*?)\", \"( +.*?)\", \"(.*?)\", \"(.*?)\", \"(.*?)\", \"(.*?)\"/g) { my $replace2 = $3; $line =~ s/02/$replace2/g; } if ($line =~ m/\"(.*?)\", \"(.*?)\", \"(.*?)\", \"(.*?)\", \"(.*?)\", +\"(.*?)\", \"(.*?)\", \"(.*?)\", \"(.*?)\", \"(.*?)\", \"(.*?)\", \"( +.*?)\", \"(.*?)\", \"(.*?)\", \"(.*?)\", \"(.*?)\"/g) { my $replace3 = $4; $line =~ s/03/$replace3/g; } if ($line =~ m/\"(.*?)\", \"(.*?)\", \"(.*?)\", \"(.*?)\", \"(.*?)\", +\"(.*?)\", \"(.*?)\", \"(.*?)\", \"(.*?)\", \"(.*?)\", \"(.*?)\", \"( +.*?)\", \"(.*?)\", \"(.*?)\", \"(.*?)\", \"(.*?)\"/g) { my $replace4 = $5; $line =~ s/04/$replace4/g; } if ($line =~ m/\"(.*?)\", \"(.*?)\", \"(.*?)\", \"(.*?)\", \"(.*?)\", +\"(.*?)\", \"(.*?)\", \"(.*?)\", \"(.*?)\", \"(.*?)\", \"(.*?)\", \"( +.*?)\", \"(.*?)\", \"(.*?)\", \"(.*?)\", \"(.*?)\"/g) { my $replace5 = $6; $line =~ s/05/$replace5/g; } if ($line =~ m/\"(.*?)\", \"(.*?)\", \"(.*?)\", \"(.*?)\", \"(.*?)\", +\"(.*?)\", \"(.*?)\", \"(.*?)\", \"(.*?)\", \"(.*?)\", \"(.*?)\", \"( +.*?)\", \"(.*?)\", \"(.*?)\", \"(.*?)\", \"(.*?)\"/g) { my $replace6 = $7; $line =~ s/06/$replace6/g; } if ($line =~ m/\"(.*?)\", \"(.*?)\", \"(.*?)\", \"(.*?)\", \"(.*?)\", +\"(.*?)\", \"(.*?)\", \"(.*?)\", \"(.*?)\", \"(.*?)\", \"(.*?)\", \"( +.*?)\", \"(.*?)\", \"(.*?)\", \"(.*?)\", \"(.*?)\"/g) { my $replace7 = $8; $line =~ s/07/$replace7/g; } if ($line =~ m/\"(.*?)\", \"(.*?)\", \"(.*?)\", \"(.*?)\", \"(.*?)\", +\"(.*?)\", \"(.*?)\", \"(.*?)\", \"(.*?)\", \"(.*?)\", \"(.*?)\", \"( +.*?)\", \"(.*?)\", \"(.*?)\", \"(.*?)\", \"(.*?)\"/g) { my $replace8 = $9; $line =~ s/08/$replace8/g; } if ($line =~ m/\"(.*?)\", \"(.*?)\", \"(.*?)\", \"(.*?)\", \"(.*?)\", +\"(.*?)\", \"(.*?)\", \"(.*?)\", \"(.*?)\", \"(.*?)\", \"(.*?)\", \"( +.*?)\", \"(.*?)\", \"(.*?)\", \"(.*?)\", \"(.*?)\"/g) { my $replace9 = $10; $line =~ s/09/$replace9 /g; } if ($line =~ m/\"(.*?)\", \"(.*?)\", \"(.*?)\", \"(.*?)\", \"(.*?)\", +\"(.*?)\", \"(.*?)\", \"(.*?)\", \"(.*?)\", \"(.*?)\", \"(.*?)\", \"( +.*?)\", \"(.*?)\", \"(.*?)\", \"(.*?)\", \"(.*?)\"/g) { my $replace10 = $11; $line =~ s/[^.]10/$replace10/g; } if ($line =~ m/\"(.*?)\", \"(.*?)\", \"(.*?)\", \"(.*?)\", \"(.*?)\", +\"(.*?)\", \"(.*?)\", \"(.*?)\", \"(.*?)\", \"(.*?)\", \"(.*?)\", \"( +.*?)\", \"(.*?)\", \"(.*?)\", \"(.*?)\", \"(.*?)\"/g) { my $replace11 = $12; $line =~ s/11/$replace11/g; } if ($line =~ m/\"(.*?)\", \"(.*?)\", \"(.*?)\", \"(.*?)\", \"(.*?)\", +\"(.*?)\", \"(.*?)\", \"(.*?)\", \"(.*?)\", \"(.*?)\", \"(.*?)\", \"( +.*?)\", \"(.*?)\", \"(.*?)\", \"(.*?)\", \"(.*?)\"/g) { my $replace12 = $13; $line =~ s/12/$replace12 /g; } if ($line =~ m/\"(.*?)\", \"(.*?)\", \"(.*?)\", \"(.*?)\", \"(.*?)\", +\"(.*?)\", \"(.*?)\", \"(.*?)\", \"(.*?)\", \"(.*?)\", \"(.*?)\", \"( +.*?)\", \"(.*?)\", \"(.*?)\", \"(.*?)\", \"(.*?)\"/g) { my $replace13 = $14; $line =~ s/13/$replace13/g; } if ($line =~ m/\"(.*?)\", \"(.*?)\", \"(.*?)\", \"(.*?)\", \"(.*?)\", +\"(.*?)\", \"(.*?)\", \"(.*?)\", \"(.*?)\", \"(.*?)\", \"(.*?)\", \"( +.*?)\", \"(.*?)\", \"(.*?)\", \"(.*?)\", \"(.*?)\"/g) { my $replace14 = $15; $line =~ s/14/$replace14/g; } if ($line =~ m/\"(.*?)\", \"(.*?)\", \"(.*?)\", \"(.*?)\", \"(.*?)\", +\"(.*?)\", \"(.*?)\", \"(.*?)\", \"(.*?)\", \"(.*?)\", \"(.*?)\", \"( +.*?)\", \"(.*?)\", \"(.*?)\", \"(.*?)\", \"(.*?)\"/g) { my $replace15 = $16; $line =~ s/15/$replace15/g; } print {$fh} $line; } close $original; close $fh;

    and this is my original file(just a short version, the actual one has thousands of numbers)

    "", "2.90", "3.00", "3.10", "3.20", "3.30", "3.40", "3.50", "3.60", "3 +.70", "3.80", "3.90", "4.00", "4.10", "4.20", "5v" 09 12 12 10 08 08 10 07 10 09 08 08 + 06 10 10 08 09 11 08 10 07 10 11 00 00 10 12 13 10 09 08 08 06 10 01 + 08 09 11 08 10 07 10 11 12 13 05

    I have to replace for example 00 with the value between the first "" so 00 would be empty string, 01 with the value of the second "2.90" so 01 would become 2.9 and so on... Thank you very much for all your help

      Did you look at my previous suggestion regarding the "g" regex modifier? Here's how to use it:

      #!/usr/bin/env perl use strict; use warnings; open (my $original, '<', 'data.txt') or die $!; open (my $fh, '>', 'output.txt') or die $!; my $first = <$original>; print $fh $first; my @replace = $first =~ /"([^"]*)"/g; while (my $line = <$original>) { $line =~ s/(\d{2})/$replace[$1]/g; print $fh $line; } close $original; close $fh;

      Is that concise enough for you?

        Thank you very much, this is exactly what I needed. God bless you!

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1173731]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (3)
As of 2024-04-20 01:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found