AkshaiShah
AkshaiShah

Reputation: 5939

Perl regular expression not matching

I have the following regular expression in Perl.

if ($line =~ m/DX/o) {
  printf("%s.\n", $line);
}

if ($line =~ m/.*DX\s+.*\s+.*\s+(.*)\sGB/oi) {
                printf("TRUE: %s\n", $1);
                ($dx = $1) =~s/,//g;
}

It is printing

DX 93,132 GB -- 2,145 GB 16840176 16835553.

But is not entering the 2nd regular expression. I have checked the regular expression and cannot see any errors. Could anyone advise?

Thanks.

Upvotes: 1

Views: 196

Answers (3)

ThisSuitIsBlackNot
ThisSuitIsBlackNot

Reputation: 24063

If all of your fields are delimited by multiple spaces (i.e. none of the fields can contain two or more spaces in a row), I would recommend using split instead of a regex:

if ($line =~ /^DX/) {
    my @fields = split /\s{2,}/, $line;
    $fields[3] =~ s/,//g; # Strip commas from 4th field
    print $fields[3];
}

__END__
2145 GB

If your data is actually tab delimited, change the split to

my @fields = split /\t/, $line;

Upvotes: 2

Sabuj Hassan
Sabuj Hassan

Reputation: 39365

Try this one:

^\s*DX.*?([\d,]+)\sGB(?!.*GB)/oi

Upvotes: 0

amaslenn
amaslenn

Reputation: 805

Follow should work: /--\s+(\S+)\s/

Upvotes: 0

Related Questions