Search code examples
regexgoogle-sheetsregex-replace

Google Sheets REGEXREPLACE... after Nth occurrence


I have a list of URLs like

https://www.example.com/inn/abc_name_of_the_product_w/o_additional_information.132.jpeg

I don´t know how they include a slash in the file name... and it could be more than one time in the same file name!

I need to perform a REGEXREPLACE to replace any slash (/) in the filename by a dash (-), but not those in the URL... so it should keep up to the 4th slash in the string.

So given example should look like:

https://www.example.com/inn/abc_name_of_the_product_w-o_additional_information.132.jpeg

And a basic REGEXREPLACE(A2,"/","-") replaces ALL the slashes in the string, that is not what I want.

I've tried many alternatives, but my knowledge has a very low limit on the regex syntax, that I cannot fully understand, no matter how many references and tutorials I read about it. =(

Any help would be appreciated! =)


Solution

  • Use REGEXEXTRACT() then SUBSTITUTE()`

    =REGEXEXTRACT(A1, "^(?:[^/]*/){4}") & SUBSTITUTE(REGEXEXTRACT(A1, "^(?:[^/]*/){4}(.+)"),"/","_")
    

    enter image description here