Home » excel » regex – Avoid duplicate code in Excel IF formula code

regex – Avoid duplicate code in Excel IF formula code

Posted by: admin May 14, 2020 Leave a comment


I want to avoid duplicate code within excel formulas. Is there a method to repeat a certain code segment?


This would be especially useful when it comes to more complex or longer sections. But: everything must be in ONE formula in ONE cell. Thanks! 🙂

EDIT: This is my current code.


It strips the long ID number out of any URL of a specific CMS. So


is probably the part which occurs more than once and should be replaced for a code which does not be that duplicate-prone.

EXAMPLE: www.domain.com/path1/path2/this-is-an-article-123-dd-123456789.html –> 1234567890
EXAMPLE: www.domain.com/path1/path2/this-is-an-article-123-dd-1234567890.amp.html ->
EXAMPLE: www.domain.com/path1/this-is-an-article-1234567890.html ->

How to&Answers:

In google sheets, you could use REGEXEXTRACT to get what you want:

enter image description here

Formula in B1:



Place the complex common sub-expression in its own cell and refer to that cell.


As an alternative, you can use a Named Formula for the sub-expression:

Named Formula


So here is another way of finding the code in Excel:


Here is the formula in Cell B1 which needs to be confirmed by pressing Ctrl+Shift+Enter, then drag it down to apply across board:


For the logic behind this formula you may give a read to this article: Extract Words with FILTERXML.

Cheers 🙂

Ps. it seems that GoogleSheet has out performed Excel in some area already.