So what can Regex do?
Regex (regular expression) is a code language used to identify patterns in text and data. Once you've identified a pattern, you can choose what you want to do with it.
Should the pattern, for example, be…
- Deleted?
- Edited into something else?
- Extracted and saved for later use?
Regex has lots of uses, and really, the only limit is your imagination, so let me show you a little magic before I reveal the trick.
Changing product titles before you upload them to your webshop
Let's say you run an electronics webshop and you've just received a shipment of the latest 2019 models. The product information has been sent in an Excel spreadsheet, which you usually upload to the website to add the products.
Unfortunately, this supplier never includes their brand name in the title, and the TV's size in inches is in a column of its own.
You want your future title to go from:
“55PUS7503/12 Ultra-thin 4K TV” to “Philips 55” Ultra-thin 4K TV”
Are you going to have to correct 100 models manually in Excel before you upload the products?
The answer is NO, at least not with a little regex magic.
What all the products have in common is that their MPN number (the long number at the front) always ends with a “/” followed by 2 digits. What we're interested in is the information that comes after these digits, namely that the product is an “Ultra-thin 4K TV”.
The MPN number will be unique to each product, so “Find & Replace” won't work. They also come in different lengths, so “=RIGHT” won't come to your rescue either.
Luckily, Excel has a regex function, “=Regexextract”
The code (A2;“/d{2}s(.*)”) tells the system that whatever comes after the slash and the two digits is what you want the title to be.

Use this formula all the way down the product sheet and voilà, you've just updated 100 products faster than you could brew a cup of coffee. As shown in the image, cell A3 is highlighted, where the result of the formula is displayed.
All you need to do now is use =CONCATENATE(B2;” “;C2;” Inch “;A3) to include the data from the other rows, and you have the perfect product description.

Create lots of redirects quickly
You run a blog and have decided to get rid of the date in the URL, so your blog is no longer called:
“supersjovblog.dk/2019/08/20/Linser-er-de-nye-kulhydrater”
but instead: “supersjovblog.dk/blog/Linser-er-de-nye-kulhydrater”
How do you update your entire back catalogue of blog posts without creating a redirect for every single post? With Regex, of course!
Once again, we need to find a pattern we can work with. What all the blog posts have in common is a date in front of them, which changes depending on when the post was published.
This date “just” needs to be changed to /blog/ instead, as the ending stays the same!
With a piece of code and a redirect plugin that can handle regex expressions, we can take care of the entire blog redirect with this code: /d{4}/d{2}/d{2}/(.*) which should redirect to /blog/$1 and ta-da!
All pages with that blog structure are now redirected to their new /blog/ URL! Completely hassle-free!

Here you can see the code working on a blog example, with the before example at the top and the changed result at the bottom.
The blue part is what matches the code, and the green part is the part we want to keep.
You can also use this for regular URL changes, as long as the ending is the same. Take a dentist's website as an example:
/tandbehandlinger/ to /behandlinger/ would be: which should redirect to: /behandlinger/$1
But again, this only works if the end of the URL is the same in both places!
Redirect your webshop and its many products
Your new webshop design is about to go live, which means that not only will all your products be in a new location, but their ending also changes from “/” to “.html”
So you're now going from:
denvildeplakatshop.dk/produkt/zebra-plakat/
to:
denvildeplakatshop.dk/shop/zebra-plakat.html
Without Regex, you'd be sitting there creating redirects by hand until you had no hair left to pull out. But once again, Regex comes to the rescue.
All you need are these Regex expressions:
/produkt/(.*)/$ captures the product name within its slashes.
/shop/$1.html changes “produkt” to “shop”, inserts the product title and adds “.html” at the end.
Plug the code into the same redirect plugin that supports Regex, and tick the “Regex” box. You're up and running.
With a single line of code, you've just moved your entire webshop and saved yourself and your employees lots of overtime and tedious grunt work!

Revealing the Regex magic trick
They say a magician never reveals their tricks… but now I have, so you can play magician at your workplace too and save people both time and money!
Throughout this blog, I've used expressions such as:
/
d{2} and d{4}
(.*)
$1
s
… But what do these expressions mean, and what do they do?
In short, these expressions tell the system what you're looking for. The first expression, used for the products, was as follows:
/d{2}s(.*)
Broken down, it looks like this:
/ simply means /
d{2} actually contains two pieces of information. “d” means there must be a digit, and “{2}” stands for the number of them, in this case 2, so here we're saying there must be two digits.
s stands for “space”
(.*) this also contains two pieces of information. The full stop on its own means “any character”.
The character that follows determines how many characters we're looking for, in this case “*”, which stands for anywhere between 0 and unlimited. So “.*” is all characters. The brackets around “.*” simply tell the system that we want to save whatever is inside them for later.
The way we then retrieve it is with the dollar sign and the order of the brackets. $1 is the first in the sequence.
Once all of these are broken down and translated, the code (/d{2}s(.*)) says that
whatever comes after a slash, followed by 2 digits and a space, is what we want to save for later.
After the show
Now that you've been spellbound and want to learn more than my short reveal, you can visit this page: https://regex101.com/.
Here you'll find all the variables, and you have a safe place to test your magic!
If magic tricks aren't quite your thing, but you'd like to save yourself and your colleagues time and money, you're of course welcome to contact the professional magicians at amplify.dk on 70 60 50 28 or info@amplify.dk