Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Andrzejek on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

regex issue

Status
Not open for further replies.

Haunter

Programmer
Jan 2, 2001
379
US
I need to get a piece of data out of an array elemet using a regex but cant seem to get the syntax right.
Now the data in the array elements is as follows

33.3 kb/s (6.5%)

I am trying to grab the 6.5% on each element. Note the numerical data within the parathesies changes but holds the same format. I have tested the regex and I think it is correct but I am not getting the data.

Code:
                my @ary1 =  split /,/;
                for (my $i = 3; $i < @ary1; $i++){
                	 #print $ary1[$i].'0\n';
                 my $tt= $ary1[$i] =~ m!\({1}\d*\.\d*%\){1}!;
                               print $tt . '\n';
                               
                               print $1;                              
                }

thanks


haunter@battlestrata.com
 
As A follow up note. I know that the data is in the array.
It is read from a file

Code:
            open (INPUT, $file)         || die "\nFAILED SLURP! $file: $!\n";
            while (<INPUT>) {

                my @ary1 =  split /,/;
                for (my $i = 3; $i < @ary1; $i++){
                	 #print $ary1[$i].'0\n';
                 my $tt= $ary1[$i] =~ m!\({1}\d*\.\d*%\){1}!;
                               print $tt . '\n';
                               
                               print $1;                              
                }

             }


haunter@battlestrata.com
 
Code:
#!perl
use strict;
use warnings;

my @ary1 =  <DATA>;
for (my $i=0; $i<@ary1; $i++){
    [b]my ($tt) = $ary1[$i] =~ /(\d+\.\d+%)/;[/b]
    print $tt , "\n";
}

__DATA__
33.3 kb/s (6.5%)

 
Thanks,

Staring at the problem I did not see the paraentheses issue. Keen eyes mikevh


haunter@battlestrata.com
 
A slight alteration to that would be:
Code:
    my ($tt) = $ary1[$i] =~ /\((\d+\.\d+%)\)/;
I've changed it so that it only matches x.x% when it appears in parentheses - mike's regexp will match x.x% anywhere.
 
You should also escape the % within the expr.

/\((\d+\.\d+\%|\d+\%)\)/

I added |\d\% in case you have whole number percentages without any decimals.


Michael Libeson
 
AFAIK Hashes aren't interpolated in regexps (for some reason), so there's no need to escape the `%' - that only applies to `@' and `$'.
 
ishnid, True statement, but I think in this case, it is better to standardly escape all variable tags so that there is no confusion and so if in the event it is changed in a later release of PERL, you do not need to worry about why the code no longer works. At least in the case of the % symbol. It does not hurt to have the \ char.


Michael Libeson
 
Does a hash ever get interpolated, even from within strings? It has a scalar context of bucket usage, but that wouldn't be useful in interpolation.

________________________________________
Andrew

I work for a gift card company!
 
I'd never actually checked - only to see that % wasn't in need of escaping in a regexp. Having tested it now, it seems that a hash won't get interpolated in a normal double-quoted string either.
 
yeah, I generally try to only escape what I need to, else it tends to add extra noise and hinders legibility a bit.

And using an | in a regex is generally a bad idea, as they are slow (as mentioned in Mike's faq219-3057). You're better off using two regexes or just reorganizing it, like [tt]/\((\d+(?:\.\d+)?%)\)/[/tt]

________________________________________
Andrew

I work for a gift card company!
 
Status
Not open for further replies.

Similar threads

Part and Inventory Search

Sponsor

Back
Top