Regex - Digital magic for process optimization
There's a lot of back-breaking work involved in sorting, cleaning and changing data.
For example, have you ever had to...
Upload a new series of products where the titles haven't been exactly as you wanted them?
Restructure your website and create redirects for all your URLs?
Move your website and create redirects for all your 4000 products and their variants?
No more fearing the slow death!
What if I said that I could solve each of these problems with just one line of code?
Let me show you the geeky magic better known as Regular Expressions or Regex.
- Last updated:
So what can Regex do?
Regex (regular expression) is a coding language used to identify patterns in text and data. Once you have identified a pattern, you can choose what to do with it.
For example, should the pattern...
- Deleted?
- Edit to something else?
- Take out and save for later use?
Regex has many uses and the sky's the limit, so let me show you some magic before I reveal the trick.
Changing the title of products before uploading them to your online shop
Let's say you run an electronics webshop and you just received a shipment of the latest 2019 models. The product information has been sent in an excel sheet that you usually upload to the website to load the products.
Unfortunately, this supplier never includes their brand name in the title and the inches of the TV are also in a column by themselves.
You would like your future title to change from:
"55PUS7503/12 Ultrathin 4K TV" to "Philips 55" Ultrathin 4K TV"
Are you about to manually correct 100 models in excel before uploading the products?
The answer is NO - at least not with a little regex magic.
Common to all products is that their MPN number (the long number in front) always ends with a "/" followed by 2 digits. What we're interested in is the information that comes after these numbers, namely that the product is an "Ultra-thin 4K TV".
The MPN number will be unique for each product, so "Find & Replace" won't work. They also come in different lengths, so "=right" is not your savior either.
Luckily excel has a regex function, "=Regexextract"
Koden (A2;“\/\d{2}\s(.*)”) fortæller systemet, at hvad end der kommer efter skråstregen og de to cifre, er hvad du gerne vil have, at titlen bliver.
Apply this formula down the product sheet and voila, you've just updated 100 products faster than you could brew a cup of coffee. As illustrated in the image, field A3 is marked where the result of the formula is shown.
All you need now is to use =COMBINATION(B2;"" ";C2;" Inches ";A3) to include the data from the other rows and you have the perfect product description.
Create many redirects quickly
You run a blog and have decided to get rid of the date markup in the URL so your blog is no longer called:
"supersjovblog.dk/2019/08/20/Linser-er-de-nye-kulhydrater"
but instead is called: "supersjovblog.dk/blog/Linser-er-de-nye-kulhydrater"
How do you update your entire back catalog of blog posts without creating a redirect for each post? By using Regex of course!
Now we need to find a pattern to work with again. Common to all blog posts is that they have a date in front of them, which changes depending on when the post was published.
This date should "just" be changed to /blog/ instead, as the ending remains the same!
Med et stykke kode, og et redirect plugin som kan klare regex-udtryk, kan vi klare hele redirecten af bloggen med denne kode: \/\d{4}\/\d{2}\/\d{2}\/(.*) som skal redirecte til /blog/$1 og tada!
All pages with the mentioned blog structure will now be redirected to their new /blog/ URL! Completely worry-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, the green part is the part we want to save.
You can also use this for regular URL changes, as long as the ending is the same. Take a dental website as an example:
/dental treatments/ to /treatments/ would be: which should be redirected to: \/treatments\/$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 going live, which means that all your products will not only be in a new place, but their endings will also change from "/" to ".html"
So you now go from:
denvildeplakatshop.dk/produkt/zebra-plakat/
to:
denvildeplakatshop.dk/shop/zebra-plakat.html
Without Regex, you would have to manually create redirects until you had no more hairs to pull out. But once again, Regex comes to your rescue.
Just use these pieces of Regex expressions:
\/product\/(.*)\/$ captures the product name within its slash.
/shop/$1\.html changes "product" to "shop", inserts the product title and adds ".html" at the end.
Plot the code into the same redirect plugin that can take Regex and check the "Regex" box. You are now up and running.
With a single line of code, you've just moved your entire webshop and saved yourself and your employees a lot of overtime and tedious backbreaking work!
The unveiling of the Regex magic trick
They say a magician never reveals his tricks... but I've done that now - so you too can be a magician in your workplace and save people time and money!
Throughout this blog, I've used terms like:
\/
\d{2} and \d{4}
(.*)
$1
\s
... But what do these terms mean and what do they do?
In short, these terms tell the system what you are looking for. The first term related to the products was as follows:
\/\d{2}\s(.*)
Broken down, it looks like this:
\/ just means /.
\d{2} is actually two-part information. "\d" means that there must be a number and "{2}" stands for the number, in this case 2, so here we say that there must be two numbers.
\s stands for "space"
(.*) this is also two-part information. The period itself means "Any character".
The following character determines how many characters we are looking for, in this case it is "*" which stands for between 0 and unlimited. So ".*" is all characters. The brackets around the ".*" just tell systems that what's inside them we want to save for later.
The way we then get 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(.*)) tells us that
whatever comes after a slash followed by 2 numbers and a space, we are interested in saving for later.
After the show
Now that you've been mesmerized and want to learn more than my short reveal, 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 are not your thing, but you want to save yourself and your colleagues time and money, feel free to contact the professional magicians at amplify.dk on +45 70 60 50 28 or info@amplify.dk
Gain deeper insights
Whether you're a generalist or a marketing specialist, our specialists have put together some great advice for you on our blog.