Quote of the Day
May I give you a word of advice? Next time you invade Italy, do not start at the bottom.
— General Fridolin von Senger und Etterlin, WW2 German general in Italy during WW2. This was his post-war advice to a British historian. The Allied Italian Campaign was a disaster in many ways – General von Senger und Etterlin's critique of the Allied effort was probably correct.
Because many corporations are focused on the Microsoft Office suite of tools, businesses often require that I use Excel/Power Query for my analysis work so that they can work with the tools I develop after I am gone. Fortunately, I really enjoy using Power Query, but I find it irritating that it does not support regular expressions. I must admit that Power Query's standard functions can do a good job of extracting strings, but the process is a bit tedious. I have a large library of regular expressions for extracting email addresses, phone numbers, social security numbers, and the like that would be efficient for me to use if Power Query could run them.
My M-code example is included in this workbook and is shown below. I used a US Census table that I copied from the Wikipedia for an application example. I use this table all the time for grouping US state data by region. While it is a small table that could have been cleaned up with only Power Query's editor (i.e., no coding), using regular expressions resulted in a tad shorter query.
Reg Ex Query
The Power Query query that contains the regex is shown below.
Modified version of regex routine documented by Imke Feldman at
x : String to be searched
y: search regex
z: replacement regex
Source = Web.Page(
"<script>var x="&"'"&x&"'"&";var z="&"'"&z&
"'"&";var y=new RegExp('"&y&"','g');
Wikipedia Description of US Census Regions and Divisions
This is the text I copied from the Wikipedia.
Region 1: Northeast
Division 1: New England (Connecticut, Maine, Massachusetts, New Hampshire, Rhode Island, and Vermont)
Division 2: Mid-Atlantic (New Jersey, New York, and Pennsylvania)
Region 2: Midwest (Prior to June 1984, the Midwest Region was designated as the North Central Region.)
Division 3: East North Central (Illinois, Indiana, Michigan, Ohio, and Wisconsin)
Division 4: West North Central (Iowa, Kansas, Minnesota, Missouri, Nebraska, North Dakota, and South Dakota)
Region 3: South
Division 5: South Atlantic (Delaware, Florida, Georgia, Maryland, North Carolina, South Carolina, Virginia, District of Columbia, and West Virginia)
Division 6: East South Central (Alabama, Kentucky, Mississippi, and Tennessee)
Division 7: West South Central (Arkansas, Louisiana, Oklahoma, and Texas)
Region 4: West
Division 8: Mountain (Arizona, Colorado, Idaho, Montana, Nevada, New Mexico, Utah, and Wyoming)
Division 9: Pacific (Alaska, California, Hawaii, Oregon, and Washington)
Tidied Table of US State Regions and Divisions
This is a tidy version of the Wikipedia data.
Cleaned Up Table of US States By Region and Division
|Midwest||East North Central||Illinois|
|Midwest||East North Central||Indiana|
|Midwest||East North Central||Michigan|
|Midwest||East North Central||Ohio|
|Midwest||East North Central||Wisconsin|
|Midwest||West North Central||Iowa|
|Midwest||West North Central||Kansas|
|Midwest||West North Central||Minnesota|
|Midwest||West North Central||Missouri|
|Midwest||West North Central||Nebraska|
|Midwest||West North Central||North Dakota|
|Midwest||West North Central||South Dakota|
|Northeast||New England||New Hampshire|
|Northeast||New England||Rhode Island|
|South||South Atlantic||North Carolina|
|South||South Atlantic||South Carolina|
|South||South Atlantic||West Virginia|
|South||East South Central||Alabama|
|South||East South Central||Kentucky|
|South||East South Central||Mississippi|
|South||East South Central||Tennessee|
|South||West South Central||Arkansas|
|South||West South Central||Louisiana|
|South||West South Central||Oklahoma|
|South||West South Central||Texas|