Kermit
Kermit

Reputation: 34054

Isolating URL paths with capturing groups

Is it possible to have n capture groups?

For example,

http://www.example.com/first-path
http://www.example.com/first-path/second-path
http://www.example.com/first-path/second-path/third-path
http://www.example.com/something.html
http://www.example.com/first-path?id=5

I am trying to capture first-path as group 1, second-path as group 2, and third-path as group 3 using http:\/\/(.*)\/(?!.*\/$)(.*), but it does not split the segments.

No specific programming language being used.

Upvotes: 0

Views: 81

Answers (1)

Nick
Nick

Reputation: 147196

If you were using PHP, you could do something like this. The first split removes the leading http://www.example.com/ part, and the second then splits those values around the /:

$urls = array('http://www.example.com/first-path',
'http://www.example.com/first-path/second-path',
'http://www.example.com/first-path/second-path/third-path',
'http://www.example.com/something.html',
'http://www.example.com/first-path?id=5');

foreach ($urls as $url) {
    $tail = preg_split('#https?://[^/]+/#', $url, -1, PREG_SPLIT_NO_EMPTY)[0];
    $paths = preg_split('#/#', $tail);
    print_r($paths);
}

Output:

Array
(
    [0] => first-path
)
Array
(
    [0] => first-path
    [1] => second-path
)
Array
(
    [0] => first-path
    [1] => second-path
    [2] => third-path
)
Array
(
    [0] => something.html
)
Array
(
    [0] => first-path?id=5
)

A similar thing could be done in Javascript:

let urls = ['http://www.example.com/first-path',
'http://www.example.com/first-path/second-path',
'http://www.example.com/first-path/second-path/third-path',
'http://www.example.com/something.html',
'http://www.example.com/first-path?id=5'];
console.log(urls.map(s => s.split(/https?:\/\/[^\/]+\//)[1].split(/\//)))

Output:

Array(5) […]    ​
  0: Array [ "first-path" ]
  1: Array [ "first-path", "second-path" ]
  2: Array(3) [ "first-path", "second-path", "third-path" ]
  3: Array [ "something.html" ]
  4: Array [ "first-path?id=5" ]

Upvotes: 1

Related Questions