How can I get my two regex subexpressions to match repeatedly?
July 12, 2009 10:42 PM
Subscribe
Regular Expressions: How can I get my two subexpressions to match repeatedly? Right now my pattern matches the first instances but refuses to match anything after it.
I've spent a few hours looking at regex tutorials, examples, and fiddling with a regex tester program and I'm obviously not making the intuitive leap.
Here's
my pattern (PHP) and
my sample text.
I'm trying to pick the four sets of HREF locations and link text (the subexpressions)
out of the html soup. So far $matches contains the whole subsection, and only the first set of location/text that I want. I suspect my failure is that some portion of the expression isn't greedy. Right?
Please hope me!
posted by cowbellemoo to computers & internet (6 comments total)
$outer_pattern = '#BEGIN TODAYS NEWS CONTENT(.*?)END TODAYS NEWS CONTENT#s';
preg_match_all($outer_pattern, $input, $matches, PREG_SET_ORDER);
$link_section = $matches[0][1];
$inner_pattern = '#<a href="(.*?)".*?<b>(.*?)</b>#s';
preg_match_all($inner_pattern, $link_section, $matches, PREG_SET_ORDER);
foreach ($matches as $m) {
echo "extracted: " . $m[1] . " : " . $m[2] . "\n";
}
posted by inkyz at 11:08 PM on July 12