Regex Techniques for Manipulating Data Formats For Tableau


Rarely, if ever, does data arrive to me in the exact format that I need it to be in for efficient analysis in Tableau.  This means something has to be done to the data before I can use it.  In Part 1 of this series, I provide specific examples of real-world data format changes that had to be completed to get the data ready for Tableau.  In Part 2 of this series, I discuss additional tools and techniques that I use to accomplish related operations.

To get started, click here to download this wonderful regex cheat sheet.


Figure 0 – Download the regex cheat sheet by clicking on the graphic.

I almost always have to fix formatting issues, remove incorrect data or remove extraneous information to extract the information that I want to analyze.  This is nothing new to me since I spent over 20 years as a high-performance numerical modeler in which the model input data sources were a wide range of databases of various quality levels.

Fixing, restructuring and reformatting data is an exercise that requires a “toolbox” of techniques, much like a carpenter needs a toolbox full of tools to do his job.  When you are used to working with data files that can be billions of lines long, working with typical business data files seems relatively easy at times, but sometimes there are a fair share of challenges that arise.  This post gives some solutions to common problems that I have encountered through the years.

If you like this article and would like to see more of what I write, please subscribe to my blog by taking 5 seconds to enter your email address below. It is free and it motivates me to continue writing, so thanks!

Enter your email address to follow this blog and receive notifications of new posts by email.

Join 693 other followers

Motivation for this post

I have used Tableau for over seven years on a daily basis, with over 1,300 distinct sources of data.  These data sources have come from a wide range of industries including scientific disciplines, education, healthcare, finance, retail, manufacturing, telecommunications, and many other businesses.

These data files have been structured and unstructured, ASCII flat files and delimited files, as well as a variety of database source files. Some of the data has been through extensive review and other sources contain a lot of information of questionable value.

Some of the data I have captured using web-based technologies such as API calls, which can create files having a lot of variation in the data formats captured.  Capturing Twitter data, for instance, produces data files having different languages, differences in control characters such as line feeds vs carriage return line feeds, etc.

To address the multitude of issues, you have to build your data manipulation toolbox to be able to format the source data appropriately for usage in Tableau. Additionally, you have to build a solid foundation of Tableau experience to recognize why data you have sent to Tableau is not doing what you think it should be doing.  Sometimes these types of problems can simply be traced back to how the data is formatted in the file you are sending to Tableau.

Techniques for fixing data formats

When it comes to changing data formats, I principally use a 64-bit programmer’s editor named Vedit ( to operate on ASCII data files.  I use Vedit if it is a one-time manipulation. If I have to do the work repeatedly, I use the regex parsing tool in Alteryx.

I have used Vedit for my entire professional career, and I’m still amazed on how I’m learning new techniques all the time. This editor is the only tool I have found that can perform blazingly fast operations across multiple files and files having millions or billions of lines (it can essentially handle unlimited file sizes).

Vedit has a macro language that is a bit cryptic and hard to learn but is extremely powerful and configurable. There are a wide range of pre-programmed macros that come with Vedit that allow you do some amazing things.  Searching and replacing can be performed using pattern matching techniques, regular expressions, or programmed any way you want using the macro language.  I use Vedit many times every day because it simply gets the job done better than anything else I have seen on the Windows platform, other than Alteryx, of course.

Examples of Data format changes/fixes using Vedit

What follows are a number of real-world, “data-format fixing” examples compiled from the past couple of years that were conducted in Vedit. Some the examples using pattern matching techniques and some use regular expression techniques.  These examples give some insight into the types of changes that need to be made to data files originating from a variety of sources.


Example 1: Replacing html tags with quotes to get the content between the tags. Three lines become one quote delimited line.

Source data before the change:

RRP #56: Chef AJ: From Junk Food Vegan to Whole Food Plant-Based Chef

Data after the change:

“RRP #56: Chef AJ: From Junk Food Vegan to Whole Food Plant-Based Chef”

Pattern match search and replace

Part 1: find <title>|013|010, replace with ”
Part 2: find |013|010</title> replace with “


Example 2: Removing html tags to get the text label that resides within the <a> </a> block.

Source data before the change:

<a href=”; rel=”nofollow”>Safari on iOS</a>

Data after the change:

Safari on iOS

Regular expression search and replace

Search for <{[^>]+}>
Replace with nothing


Example 3: Removing spurious <LF> characters that show up in the Twitter tweet field when capturing Twitter data with their API. These arise because people use many different computing platforms, apps and operating systems when submitting Tweets to Twitter.

Data Before Cleaning (3 lines with two spurious <LF> characters):

“Hyper realism illustration for the Wall (2011) by COCOCHOE<LF>


#drawing #dessin #? #?? #tableau #painting…;

Data After Cleaning (1 line, no extra <LF> characters):

“Hyper realism illustration for the Wall (2011) by COCOCHOE #drawing #dessin #? #?? #tableau #painting…;


  1. This operation will take two passes to fully clean the file;
  2. Reg expression search (finds anything before a <LF> other than a <CR>, which keeps it off the end of the line sequence)

Part 1 Using Reg Expression Search/Replace:  Clean <LF> anywhere, but leaves <LF> at end of lines
Search {[^\h0D]}{\h0A}
Replace \1

Part 2 – get rid of <LF> remaining at the end of lines once Part 1 is completed
Search {\h0A}$
Replace Nothing


Example 4: Cleaning the Twitter API statuses field to gain access to the Twitter profile

Data Before Cleaning:

Data After Cleaning:

Regular expression search and replace

Search /statuses/[0-9]+
Replace nothing


Example 5: Swap columns 1 and 3 in a comma delimited ASCII file and reformat the date field that is going to column 1.

Data Before Swap:

“ALBUQUERQUE-SANTA FE”,0000857996,11-OCT-12,11216.61,2790.2,80
” “,0000811843,01-APR-12,3580.63,730.9,3

Data After Swap:


OCT/11/2012,0100807996,”ALBUQUERQUE-SANTA FE”,11216.61,2790.2,80

APR/01/2012,0000811843,” “,3580.63,730.9,3

Regular Expression Search  and Replace

Search for ^{“}{[^”]+}”,{[0-9]+},{[0-9]+}-{[A-Z]+}-{[0-9]+}

1 2 3 4 5 6

1 is first quote
2 is anthing up to the next quote
3 is the 10 digit integer
4 is the DD (DAY)
5 is the MMM (MONTH)
6 is the YY (YEAR)

Replace with



Example 6: Reformat dates in a very big, multi-million line file

Data Before Change:


Data After Change;


Here are all 12 month replacement codes (for dates beyond the year 2000):














Example 7: Get rid of ” ” around 5 comma delimited numbers in a row, that have various formats

Data Before Change


Data After Cleaning:


Regular Expression Search and Replacements

Note: Multiple Passes Required to Handle Multitude of Formats

Start with

Search “{[0-9]+}”,”{[0-9]+}”,”{[0-9]+}”,”{[0-9]+.[0-9]+}”,”{[0-9]+.[0-9]+}”
Replace \1,\2,\3,\4,\5 (used with all the remaining examples)

For the fifth number with only two decimal digits

Search “{[0-9]+}”,”{[0-9]+}”,”{[0-9]+}”,”{[0-9]+.[0-9]+}”,”{.[0-9]+}”

For the fourth number as int and fifth number with only two decimal digits

Search “{[0-9]+}”,”{[0-9]+}”,”{[0-9]+}”,”{[0-9]+}”,”{.[0-9]+}”

For negatives in the fifth number

Search “{[0-9]+}”,”{[0-9]+}”,”{[0-9]+}”,”{[0-9]+.[0-9]+}”,”{-[0-9]+.[0-9]+}”

For negatives in the fifth number with only two decimal digits


For integers in the fifth number

Search “{[0-9]+}”,”{[0-9]+}”,”{[0-9]+}”,”{[0-9]+.[0-9]+}”,”{[0-9]+}”

Example 8: Change date format at the end of a line from yyyymmdd to mm/dd/yyyy

Data Before the Change:

“0003405855”,”S10422″,”2004 Fall-2005 Calendar”,”Mail”,”20040903″
“0025005555”,”S09904″,”2008 Fall-2009 Calendar Sale”,”Calendar”,”20081015″

Data After the Change:

“0003405855”,”S10422″,”2004 Fall-2005 Calendar”,”Mail”,09/03/2004
“0025005555”,”S09904″,”2008 Fall-2009 Calendar Sale”,”Calendar”,10/15/2008

Regular Expression Search and Replace

Search {“}{[0-9][0-9][0-9][0-9]}{[0-9][0-9]}{[0-9][0-9]}{“}$
Replace \3/\4/\2

Example 9: Change date format in the middle of a comma separated line from yyyymmdd to mm/dd/yyyy

Data Before Change:


Data After Change:


Regular Expression Search and Replace

Search  {,}{[0-9][0-9][0-9][0-9]}{[0-9][0-9]}{[0-9][0-9]}{,}
Replace \1\3/\4/\2\5

Final Thoughts

The examples shown above illustrate how a powerful programmer’s editor can be used to change data formats, swap columns, extract information from unstructured files, and convert date formats. There are a lot of other examples I could provide, but these may give you ideas of how to attack problems you may be having.

In part 2 of this series, I’ll discuss how I use Alteryx to accomplish some tasks that make working with data in Tableau much easier on a daily basis.

One thought on “Regex Techniques for Manipulating Data Formats For Tableau

  1. Pingback: How to Extract Hyperlinks From Excel Formulas To Use In #Tableau Dashboards | 3danim8's Blog

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s