How to find the text between the second and fourth slashes in a path like /folder/subfolder-1/subfolder-2/subfolder-3
? I’m trying to replace this with something like /folder/new-folder/subfolder-3
.
The most important for me is to be able to find the part after the n-th slash.
I tried the regex /((.*?)/){3}
, but it doesn’t work.
Using Match resetter \K
meta-character you are able to do it in a simpler way.
Find:
/.*?/\K(.*?/){2}
Replace with:
new-folder/
One way you could to it is by using this string in the pattern to replace
(/.+?)(/.+?){2}(/\S+)
And use this one in your pattern to replace it with
$1/new-folder$3
From your string:
/folder/subfolder-1/subfolder-2/subfolder-3
(/.+?)
will match /folder
as $1
(/.+?){2}
will match /subfolder-1/subfolder-2
as $2 (not used)
(/\S+)
will match everything that isn't a space, in this case/subfolder-3
as $3
Leaving you room to insert your new-folder in-between.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With