The Computer Oracle

Is there a way to use Regex( or comparable solution) in Excel formulas?

--------------------------------------------------
Hire the world's top talent on demand or became one of them at Toptal: https://topt.al/25cXVn
and get $2,000 discount on your first invoice
--------------------------------------------------

Take control of your privacy with Proton's trusted, Swiss-based, secure services.
Choose what you need and safeguard your digital life:
Mail: https://go.getproton.me/SH1CU
VPN: https://go.getproton.me/SH1DI
Password Manager: https://go.getproton.me/SH1DJ
Drive: https://go.getproton.me/SH1CT


Music by Eric Matyas
https://www.soundimage.org
Track title: Cool Puzzler LoFi

--

Chapters
00:00 Is There A Way To Use Regex( Or Comparable Solution) In Excel Formulas?
01:30 Accepted Answer Score 11
02:16 Answer 2 Score 2
02:40 Answer 3 Score 3
03:39 Answer 4 Score 1
04:45 Thank you

--

Full question
https://superuser.com/questions/821498/i...

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#microsoftexcel #microsoftexcel2010 #regex

#avk47



ACCEPTED ANSWER

Score 11


Excel does not have regular expressions, but you can certainly build up most of what they can do one formula at a time. Here are some examples:

Extract first space-delimited word:

B1: =FIND(" ", A1)
C1: =LEFT(A1, B1 - 1)

Check if the first word is all non-lowercase:

D1: =EXACT(UPPER(C1), C1)

Extract second space-delimited word:

E1: =FIND(" ", A1, B1 + 1)
F1: =MID(A1, B1 + 1, E1 - B1 - 1)

Extract 4-digit year if possible:

G1: =LEFT(F1, 4) - 0

You can create as many of these extractions and tests as you need to become convinced that the data in A1 matches the ticker format, and not the header format, and then use something like this to reproduce that cell only in the right cases:

H1: IFERROR(IF((B1+E1+LEN(C1)+LEN(F1)+G1+D1)*0=1,"",A1),"")

spreadsheet showing helper columns




ANSWER 2

Score 3


The accepted answer is good. I will add that for RegEx specifically, you could pass a range to a VBA function and then evaluate its value using RegEx.

  1. Add a reference to "Microsoft VBScript Regular Expressions 5.5"

How to use Regular Expressions (Regex) in Microsoft Excel both in-cell and loops

  1. Create a function that takes a range and then evaluates it using RegEx.

Call the function from an Excel formula -- the user doesn't have to know VBA, only the name of the function.

The output in Excel will update in real-time, so long as the function takes a range and not, say, a string. (Excel will know when the range has been modified, whereas if it's just a string passed from a cell, it won't know.)

Or you could also create a button or something that calls the VBA and does the filtering. That would satisfy the requirement that the user doesn't need to know VBA.




ANSWER 3

Score 2


I would try Advanced Filters - you can set up a table of criteria using wildcards, and then point a Filter operation at it. You can find the Advanced button on the Data Ribbon, in the Sort & Filter section (in Excel 2013).

There's a very detailed walkthrough here:

Advanced Filters: Excel’s Amazing Alternative To Regex




ANSWER 4

Score 1


One can also combine the approach provided in the accepted answer (i.e. FIND and extract with LEFT or RIGHT) with an array formula to mimic some regexp features without using VBA in some specific cases.

The original post mentions a regexp containing the bracket expression [A-Z], which can be represented by the array of strings {"A","B",...,"Z"}. So using the array form of FIND, we can test the input against a series of strings and get the results as an array.

For example, assuming we want to extract street names from addresses

Baker street 4 W
Bäckerstrasse 14
Rue des Boulangers 4a

a solution is to find any first white space followed by a digit, i.e. ' [0-9]' and cut out the string starting from that position. So with an input located in M14,

FIND(" "&{0;1;2;3;4;5;6;7;8;9};M14)

will return an array of position or #NA if not found. Adding an IFERROR around the FIND makes sure that the strings that are not found return 0

IFERROR(FIND(" "&{0;1;2;3;4;5;6;7;8;9};M14);0)

Assuming the strings that are being matched are mutually exlclusive we can SUM to get the position of ' [0-9]'. Finally shortening the string at given position, the final formula is

{=LEFT(M14;SUM(IFERROR(FIND(" "&{0;1;2;3;4;5;6;7;8;9};M14);0)))}