Take last section of URL and return it in Excel

Viewed 753

My problem I need to solve is to rip apart the last section of a URL.

I have the URL

.../one/two/three/four

and need to have

four

OR

/four

I used Excel function

=RIGHT(F2;SEARCH("/";F2))

But it returns

r

Anyone have a solution for this? Thanks!

2 Answers

You can do that using FILTERXML (available from Excel 2013 and later):

=FILTERXML("<t><s>"&SUBSTITUTE(A1;"/";"</s><s>")&"</s></t>";"//s[last()]")

This formula will first build an XML string like this

<t><s>...</s><s>one</s><s>two</s><s>three</s><s>four</s></t>

and then extract the last node.

There has recently been a nice question here on Stackoverflow that I highly recommend to read:

Excel - Extract substring(s) from string using FILTERXML

Related