1

I've got the URLs in the following style:

http://whatever.com/param1/val1/param2/val2

I want to match all key/value pairs. I tried this pattern:

/^http:\/\/whatever.com(?:\/([^\/]+)\/([^\/]+))*$/g

It only matches the last key/value pair.

Unfortunately, I cannot use code to get the pairs... How can I capture all pairs?

Kobi
  • 135,331
  • 41
  • 252
  • 292
jney
  • 1,862
  • 2
  • 17
  • 21
  • 2
    Why not just `split` on the forward slash? – RoccoC5 Aug 25 '11 at 09:22
  • @RoccoC5 you mean by coding ? because it should be processed only with a regex – jney Aug 25 '11 at 09:32
  • 1
    What programming language is this? And why "should it only be processed with a regex"? In this case a non-regex solution would most likely be much simpler. – RoToRa Aug 25 '11 at 10:09
  • @RoToRa : * "What programming language is this?" : Just generic Regex * "And why "should it only be processed with a regex"?" : Because it is supposed to work with a client interface only working with regex * "In this case a non-regex solution would most likely be much simpler" : yes, for sure, it would be simpler – jney Aug 25 '11 at 12:00
  • @jney: There is no such thing as "generic regex", each implementation has it's differences, and the environment is important, because what you want is most likely not possible with a simple one-time application of a regex. – RoToRa Aug 25 '11 at 12:26
  • 1
    I see nothing recursive in this description. Why the tag? – tchrist Aug 25 '11 at 13:02
  • @RoToRa : i'm not if php or java is used as server side language – jney Aug 25 '11 at 16:16

1 Answers1

0

Try making your match non-greedy by adding a ? after the *:

/^http:\/\/whatever\.com\/(?:([^\/]+)\/([^\/]+)\/?)*?$/g
Bohemian
  • 412,405
  • 93
  • 575
  • 722
  • 2
    i still match only the last couple of results – jney Aug 25 '11 at 09:41
  • 1
    Please please please write that with a different delimiter, and `/x`, so you can read it: `m{ ^ http: // whatever \. com / (?: ( [^/] + ) / ( [^/]+ ) /? ) *? $ } xg`. The LTS ( “leaning toothpick syndrome” ) is really really bad here: I see a giant DOUBLEYOU in front of *whatever*: `\/\/`, which I doubt works as an appreciation for *www*. :) It probably should have newlines and indentation, too. Please make regexes one can read. BTW, that pattern won’t actually pull out all key-value pairs just because you used `/g` or quantified them, you know. – tchrist Aug 25 '11 at 19:54