Reputation: 4786
I'm struggling to get the right regex to match the following;
content/foo/B6128/8918/foo+bar+foo
OR
content/foo/B6128/8918/foo+bar+foo/randomstringnumsletters
I'm sure this isn't that complicated and I'm nearly there, just can't get it perfected. Here's what I've tried;
content\/(\w+)\/(\w+)\/(\d+)\/([^\/]+[\w]+)\/?(\w*)$
using this online tester: http://regex101.com/r/sB8rR5/2
It still matches a 5th item with this string content/foo/B6128/8918/foo+bar+foo
;
And while technically this pattern does match either OR url structures. I don't want it to match the 5th item when there's no randomstringnumsletters
present.
After playing around with it for a bit, I do realise some elements are redundant with what I've tried, but I'm not getting anywhere with it...
Upvotes: 1
Views: 160
Reputation: 174696
Just turn the last capturing group into an optional one, and change \w*
to \w+
in the last capturing group inorder to prevent null character to be captured by the 5th group.
content\/(\w+)\/(\w+)\/(\d+)\/([^\/]+[\w]+)\/?(\w+)?$
Upvotes: 2
Reputation: 423
You can take each part as an array, then take the part that you need...
Upvotes: 0
Reputation: 54163
Looks like your REAL pattern should be:
content\/((?:\w+\/?)+)
or am I wrong? This will match the whole string (after content/
) and return it all /
delimited. You can parse each variable from there.
Upvotes: 0