Click to See Complete Forum and Search --> : PERL Regexp Difficulties -- Capture Groups and Global


bluestars
08-19-2007, 02:23 AM
Hello all. I just recently began my foray into the world of perl. I was lured in by LWP, to be honest. Why can't PHP have something like that?

Anyway, here's my goal. I've got a string (in $response->decoded_content), and a rexex (/([0-9,]*)g<\/td><td><a href="id=([0-9]*)&"/g; (Okay, so that's not the real regex, but it has the key features: the capture groups.)

There are 20-ish instances of this pattern in my string. I would like to end up with two arrays (@one and @two, let's call them), each containing the data from each capture group.

Any idea on how to do this?

EvilCasey™
09-10-2007, 08:48 PM
push (@one, $1);
push (@two, $2);

Each pattern match (grouped within the parens) in your regexp is back referenced into the magical numerical variables corresponding to the groups placement in the regexp from left to right. so (group1) will be in $1 and (group2) will be in $2. PHP has a builtin function called fget("http://somedomain/somefile") which retrieves documents over HTTP similar to perls LWP.