Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Regex add string after 11th instance of /

I have a collection of web addresses with the following structure:

https://www.example.com/img/public/vendor1/us/en/product-assets/vendor/product/green/device_230x400_a.gif

When constructing this data structure, I realized that I needed to add /image/ after the given product name. The product names will vary, but the slashes are fixed instances, so this should be appropriate: add /image/ after the 11th instance of /.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

In other words, aim for:

https://www.example.com/img/public/vendor1/us/en/product-assets/vendor/product/image/green/device_230x400_a.gif

I would greatly appreciate any assistance; I am using Notepad++ for replacing.

Thanks!

>Solution :

You may use the following find and replace, in regex mode:

Find:    ^(https?://(?:[^/]+/){9})(.*)$
Replace: $1image/$2

This regex says to:

^
(                     capture in $1
    https?:           http: or https:
    //                //
    (?:[^\/]+\/){9}   match 9 path components
)
(.*)                  capture the remainder of the URL in $2
$

Then we replace with $1image/$2 to insert an /image path component at the desired location.

Here is a demo.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading