1

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...

Novocaine
  • 4,197
  • 3
  • 38
  • 59

3 Answers3

2

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+)?$

DEMO

Avinash Raj
  • 160,498
  • 22
  • 182
  • 229
  • I may not have been clear enough with my question. I do want to match the 5th set if it exists. e.g. in [this example](http://regex101.com/r/sB8rR5/4) I want the first match to not return the 5th element whereas I do want it to return the 5th in the second match. Does that make sense? – Novocaine Jul 16 '14 at 15:44
  • 1
    oh sorry see http://regex101.com/r/sB8rR5/6 . I think this is what you want? let me know whether it works or not. – Avinash Raj Jul 16 '14 at 15:59
0

Looks like your REAL pattern should be:

content\/((?:\w+\/?)+)

DEMO

or am I wrong? This will match the whole string (after content/) and return it all / delimited. You can parse each variable from there.

Adam Smith
  • 45,072
  • 8
  • 62
  • 94
  • That doesn't then split up the values between the slashes, kind of defeats the point of using regex in this case. I might as well use `content\/(.+)` – Novocaine Jul 16 '14 at 15:54
  • @Novocaine88 YMMV. I prefer to match all content then parse the match rather than try and create separate groups, some of which may be empty (as you're observing now) – Adam Smith Jul 16 '14 at 15:55
0

You can take each part as an array, then take the part that you need...

DEMO

cbertelegni
  • 395
  • 2
  • 10