Regular expression question
PERLarity
created: 2006-03-31 00:57:38
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.
Re: Regular expression question
created: 2006-03-31 01:11:12

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
created: 2006-03-31 01:11:27
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.
Re^2: Regular expression question
created: 2006-03-31 02:40:36
what does the m# do? m is for match, but I don't understand what the # does.
Re^3: Regular expression question
created: 2006-03-31 02:50:40

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^4: Regular expression question
created: 2006-03-31 02:52:17
learn something new everyday.... thanks!
Re^5: Regular expression question
created: 2006-03-31 03:59:10
You can choose other symbols, eg the pipe symbol '|' or you can choose bracket pairs eg m{^abc}. Note that the slash '/' is the default character and using this allows you to omit the 'm' operator, eg /^abc/. The usual reason to choosing something other than the '/' is when matching on *nix paths so that you don't have to keep escaping the '/'s, eg /\/path\/to\/file/ versus m#/path/to/file#.

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

Re: Regular expression question
created: 2006-03-31 02:37:19

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
created: 2006-03-31 12:01:21
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.

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