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

hey guy i'm a newbie at perl and facing some problem with extraction of a number using regexp into a variable. The string i need to look for is: |M| (avp-code = 268)(flags = 64)] = XXXX where XXXX is a no that i need to put in a variable.please help

Replies are listed 'Best First'.
Re: extraction using regular expression
by moritz (Cardinal) on Aug 08, 2011 at 12:25 UTC
Re: extraction using regular expression
by jethro (Monsignor) on Aug 08, 2011 at 12:31 UTC
    my ($number)= $searchstring=~/\|M\|\(avp-code = 268\)\(flags = 64\)\] += (\d+)/; #or my ($number)= $searchstring=~/\Q|M|(avp-code = 268)(flags = 64)]\E = ( +\d+)/;

    The only difficult part is escaping all the characters that have a special meaning in regexes. Easier to use \Q\E to do that automatically

      thanks.i was getting stuck with the \issue.sorry for the bother though.realised it was kind of a trivial question.
Re: extraction using regular expression
by JavaFan (Canon) on Aug 08, 2011 at 12:55 UTC
    Untested:
    ($number_newbie_needs) = "|M| (avp-code = 268)(flags = 64)] = XXXX" =~ + /\] = ([0-9]+)/;
    I'm assuming your numbers are non-negative integers in base ten.