user2935473
user2935473

Reputation: 33

Regex capture after second /

I'd like to regex capture in Perl, all characters after the second occurrence of the / character.

using the the example:

/en-us/hello/world/newyork.html

I'd like to be able to capture:

hello/world/newyork.html

I tried:

^(?:[^\/]*\/){2}([^\/]*)

Only captures hello as group 1.

Upvotes: 1

Views: 159

Answers (2)

mrzasa
mrzasa

Reputation: 23347

Try this one:

\/[^\/]+\/(.*)

Online Demo

Explanation:

  • \/[^\/]+\/ you first match everything till the second /:
    • \/ the first slash
    • [^\/]+ one or more of chars that are not a slash
    • /` the second slash
  • (.*) the substring you need is in the capturing group

Upvotes: 3

daxim
daxim

Reputation: 39158

Is this URI parsing? It looks like URI parsing.

use URI qw();
my $u = URI->new;
$u->path('/en-us/hello/world/newyork.html');
my @s = $u->path_segments;
print join '/', @s[2..@s-1];

Upvotes: 0

Related Questions