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

Trying to match lines with the following format:
Identities = 1529/1540 (99%), Gaps = 2/1540 (0%)
and take out the part where the 99% is in the example.

Can anyone show me what code would do this? Thanks.

Replies are listed 'Best First'.
Re: Regular expression question
by McDarren (Abbot) on Mar 31, 2006 at 06:11 UTC
    My interpretation of your question:

    "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.
      what does the m# do? m is for match, but I don't understand what the # does.

        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

Re: Regular expression question
by duff (Parson) on Mar 31, 2006 at 06:11 UTC

    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.

Re: Regular expression question
by gube (Parson) on Mar 31, 2006 at 07:37 UTC

    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";
Re: Regular expression question
by TedPride (Priest) on Mar 31, 2006 at 17:01 UTC
    Assuming your text is well-formed, the following should work well as a more modular solution:
    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.