Reputation: 345
I want to perform a regex using grouping. I am only interested in the grouping, its all I want returned. Is this possible?
$haystack = '<a href="/foo.php">Go To Foo</a>';
$needle = '/href="(.*)">/';
preg_match($needle,$haystack,$matches);
print_r($matches);
//Outputs
//Array ( [0] => href="/foo.php"> [1] => /foo.php )
//I want:
//Array ( [0] => /foo.php )
Upvotes: 0
Views: 97
Reputation: 383716
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.
preg
functions implement the PCRE flavor.(?=regex)
(positive lookahead): YES(?<=text)
(positive lookbehind): fixed + alternation<?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
)
)
These are mostly Java, but the regex part covers using lookarounds/assertions:
Upvotes: 2
Reputation: 816324
You could use array_shift()
:
array_shift($matches);
but there is no flag to tell preg_match
to do what you want.
Anyway, as you know that it will always be there, you should be able to handle this in your code. Why do you want / need this?
Upvotes: 1
Reputation: 97805
No. The 0 index will always be the text that was matches, not the groups. Of course, you can just remove the first element and renumber the array.
Upvotes: 1