You haven't said what's variable and what's fixed so there are any number of possible solutions. If you want to remove the 99%, you can just say $string =~ s/99%//; or if you want to remove all things that look like percentages, you could say $string =~ s/\d+%//g;. But you may want something more complicated. I don't know.
"I want to extract everything contained within the first pair of parentheses in the given string"
In that case....
#!/usr/bin/perl -w use strict; my $string = "Identities = 1529/1540 (99%), Gaps = 2/1540 (0%)"; my ($wanted) = $string =~ m#\((.*?)\)#; print "$wanted\n";That outputs 99%, but it may need to be tightened up a bit depending on how accurate my interpretation of your question is, and how much your data varies.
Hi rangersfan,
(/) This is the usual delimiter for the text part of a regular expression. you can also use (#) as delimiter. By using usual delimiter with regexp i have given the code below you can also refer that for your understand :-p
You can also choose different delimiters for doing substitution, eg s|/path/to/|| or s{abc}{xyz}. Note when using paired brackets you need a set around both the "to be replaced" and the "replace with" strings.
Cheers,
JohnGG
Hi try this,
#!/usr/local/bin/perl -w use strict; my $str = "Identities = 1529/1540 (99%), Gaps = 2/1540 (0%)"; my ($output) = $str =~ m/(\d+%)/gsi; print "$output\n";
use strict;
use warnings;
my ($text, %percent);
$text = 'Identities = 1529/1540 (99%), Gaps = 2/1540 (0%)';
$percent{$1} = $2 while $text =~ /(\w+) = \d+\/\d+ \((\d+)%\)/g;
for (sort keys %percent) {
print "$_ : $percent{$_}\n";
}
This way you can access any percentage by name.
perlmonks.org content © perlmonks.org and duff, gube, johngg, McDarren, PERLarity, rangersfan, TedPride
prlmnks.org © 2006 edmund von der burg (eccles & toad)
v 0.03