in reply to extracting variables from regex

I don't think so, apart from the famous (infamous?) $`, $&, and $', the variables which should not be named (or used).

But if you recast your regex slightly, you can get SOME information.

With a quantifier, a capturing paren shows the last element matched. But by adding another set of parens AROUND the quantifier, you can get the "grouped match".

/(\d\.?){1,4}/ # $1 is last octet matched /((\d\.?){1,4})/ # $1 is IP address, $2 is last octet matched
(Edit:You're better off using the other approaches suggested in the other responses, though)

Not really useful in this case, I admit, but then again, for your academic case all you really needed was a call to split :)

echo 1234.45.67.890 | perl -ne'printf "%X%X%X%X",split /\./'
(Edit:Juerd is right, I should have used the split in list context)
--
Mike

Replies are listed 'Best First'.
Re: Re: extracting variables from regex
by Juerd (Abbot) on Apr 02, 2002 at 22:05 UTC

    echo 1234.45.67.890 | perl -ne'split /\./; printf "%X%X%X%X",@_'

    From split's documentation:

    In scalar context, returns the number of fields found and splits into the @_ array. Use of split in scalar context is deprecated, however, because it clobbers your subroutine arguments.
    Why not have split in list context, and forget about @_?
    printf "...", split /\./

    U28geW91IGNhbiBhbGwgcm90MTMgY
    W5kIHBhY2soKS4gQnV0IGRvIHlvdS
    ByZWNvZ25pc2UgQmFzZTY0IHdoZW4
    geW91IHNlZSBpdD8gIC0tIEp1ZXJk
    

      Why have split at all?
        
      echo 192.168.0.1 | perl -nle 'printf "%02X"x4, /\d+/g'
         MeowChow                                   
                     s aamecha.s a..a\u$&owag.print
      Because... No particular reason :)

      I always do that for one liners, out of laziness.

      For any real script, I use -w and strict, and have gotten out of the bad habit.

      You're right, I shouldn't be doing it in one-liners, either.
      --
      Mike