Arthur Camberlein >> SEO & Data articles >> Find or extract the handle (last part of a URL) with a REGEX

Find or extract the handle (last part of a URL) with a REGEX

Find or extract the handle (last part of a URL) with a REGEX

This is the REGEX: [^\/]+$ you would like to use to extract the last part of a URL also known as the handle

I often use this to compare URLs that are localized and end with the same handle.

How to use [^\/]+$ REGEX in Google Spreadsheet?

To use it on a Google Spreadsheet you can enter the formula =REGEXEXTRACT() and select in first the URL or the cell where the URL is & then you can use the [^\/]+$ REGEX.

Let's say you have a column A:A with all your URLs, for A2 (A1 being the header column URL), you have the following URL https://arthur.camberlein.com/blogs/articles/remove-x-characters-from-a-cell-in-excel in the column B:B you could use the following formula on B2 to extract remove-x-characters-from-a-cell-in-excel.

=REGEXEXTRACT(A2, "[^\/]+$")

Then you can drag down B2 into the rest of the B:B column.

How does this REGEX work?

This is how it works:

  1. [^\/]: This is a character class that matches any character that is not a forward slash (/)
  2. The caret (^) at the start of the character class negates it
  3. The \/ is just a forward slash, but it needs to be escaped with a backslash because forward slashes have special meaning in regular expressions.
  4. +: This is a quantifier that means "one or more"
  5. so [^\/]+ matches one or more of any character that is not a forward slash
  6. $: This is an anchor that matches the end of the string.
Retour au blog

Blog post taggued in:Data, Tips

Related blog posts: