EXCEL: Formula to extract number string between the nth instance of character to the next specific character (retrieving ID from URL)

Viewed 101

I need to write a formula that can retrieve the number between the 4th "/" to the next "-"

Here's an example of the URL structure

https://www.example.com/category-name/1234-product-name

I've managed to get it done when the category name is ONE word like following URL with formula below. https://www.example.com/category/1234-product-name

=MID(LEFT(A2,FIND("-",A2)-1),FIND("#",SUBSTITUTE(A2,"/","#",4))+1,255)

Result = 1234

The problem is that I can't rely on the category name always being one word... And the product name can also vary in characters so i can't start from right counting the "-"

Any tips & tricks? :)

1 Answers

You need the break the problem and then solve it.

=MID(B1,FIND("/",B1)+1, IFERROR(FIND("-",B1,FIND("/",B1)), LEN(B1) + 1)  - FIND("/",B1)-1)

Explanation

=MID(TEXT, STARTING CHARACTER, NO OF CHARACTERS)

=MID(TEXT ,   WHERE FIRST '/' EXISTS   ,   WHERE '-' EXISTS   -   WHERE FIRST '/' EXISTS )

and for removing the domain

=REPLACE(A1,1,LEN("https://www.example.com/"),"")

enter image description here

Related