I'm trying to capture the middle part of a URL, that contains a conditional ending:
A URL can be of two sorts:
/a/b/(part/needed)
/a/b/(part/needed)/page/#
here's the regexp I use:
preg_match('@/a/b/(.*)(/page/\d)?@i', '/a/b/some/text/page/1', $matches);
returns
0=>"/a/b/some/text/page/1",
1=>"some/text/page/1"
It's ok but it includes the conditional ending which I don't want!
Can someone tell me how to not include the conditional string ending in it but still match when the last segment is present or absent?
By anchoring the expression with ^$
and making the first group non-greedy (.*?)
, you can get the segment you need. The .*
alone is a greedy match, and will eat up everything that follows the .*
.
preg_match('@^/a/b/(.*?)(/page/\d)?$@i', '/a/b/some/text/page/1', $matches);
//-----------^-------^^^-----------^
print_r($matches);
Array
(
[0] => /a/b/some/text/page/1
[1] => some/text
[2] => /page/1
)
If you don't need the /page/1
, make that a non-capturing group (?:...)
.
preg_match('@^/a/b/(.*?)(?:/page/\d)?$@i', '/a/b/some/text/more/page/1', $matches);
//----------------------^^^
print_r($matches);
Array
(
[0] => /a/b/some/text/more/page/4
[1] => some/text/more
)
regular-expressions.info has good information on character repetition with +
and *
, and the pitfalls of greediness.