Actually this is possible with lookarounds. Instead of:
href="(.*)">
You want
(?<=href=").*(?=">)
Now this will match (and therefore capture into group 0) any .*
that is preceded by href="
and followed by ">
. Note that I highly suspect that you really need .*?
instead, i.e. reluctant instead of greedy.
---A--Z---A--Z----
^^^^^^^^^^^
A.*Z
In any case, it looks like PHP's preg
is PCRE, so it should support lookarounds.
regular-expressions.info links
- Lookaround
- Flavor comparison
- PHP's
preg
functions implement the PCRE flavor.
- PCRE:
(?=regex)
(positive lookahead): YES
(?<=text)
(positive lookbehind): fixed + alternation
Demonstration
<?php
$haystack = '<a href="/foo.php">Go To Foo</a><a href="/bar.php">Go To Bar</a>';
$needle = '/(?<=href=").*?(?=">)/';
preg_match_all($needle,$haystack,$matches);
print_r($matches);
?>
Running this on ideone.com produces:
Array
(
[0] => Array
(
[0] => /foo.php
[1] => /bar.php
)
)
Related questions
These are mostly Java, but the regex part covers using lookarounds/assertions: