I have an URL formatted like so:
http://domain.com/post-type/category/category-name/page/2
And need to extract only category-name
no matter what follows.
For example, in the url:
http://domain.com/projects/category/print/page/2
I only want the word
print
I am trying this regex: category\/(.+)\/(?(?=(?:category)\/)^|(.+))\/(.+)\/?$
cobbled together via some searching, but I really am quite confused by Regex.
Note
I am executing this in PHP (in what I think is regex, correct?). At the moment, the Wordpress code is as so: $newrules['projects/category/(.*/?)$'] = 'index.php?post_type=project&project_cat=$matches[1]';
which seems to take the entire URL after projects/category/
Edit
Is it also possible to get the page number as well?
eg: http://domain.com/post-type/category/category-name/page/2
Is it possible to get only category-name
and 2
as separate matches?
You've to use an ungreedy matcher .+?
the ?
behind .+
means ungready and it stops matching after finding the first patterm behind itself. In this case this is /.+
.
/category\/(.+?)\/.+/
You can try this regex.
(?<=http:\/\/domain\.com\/projects\/category\/)([^\/]+)