

more than one consecutive spaces), use the same regex \s+ but replace the found matches with a single space character. To make it easier to manage your patterns, you can input the regex in a predefined cell and supply it to the formula using an absolute reference like $A$2, so the cell address will remain unchanged when copying the formula down the column. To remove all spaces in a string, you simply search for any whitespace character, including a space, a tab, a carriage return, and a line feed, and replace them with an empty string ("").Īssuming the source string is in A5, the formula in B5 is: With the RegExpReplace function added to your workbook, let's tackle different scenarios one at a time.
Regex whitespace how to#
How to remove whitespace with regex - examples For whitespace, it is irrelevant and therefore omitted.įor more information, please see RegExpReplace function. Match_case (optional) - a Boolean value indicating whether to match (TRUE) or ignore (FALSE) text case.In most cases, you'll omit it to replace all instances (default). Instance_num (optional) - the instance number.space character (" ") to replace multiple spaces with a single space character.empty string ("") to trim absolutely all spaces.To remove whitespaces, you'd set this argument to either: Replacement - the text to replace with.Text - the original string to search in.The first three arguments are required, the last two are optional.

RegExpReplace(text, pattern, replacement,, ) Here's the function's syntax for your reference:
Regex whitespace code#
To add the function to your Excel, just copy its code from this page, paste it in the VBA editor, and save your file as a macro-enabled workbook (.xlsm). Wait, why "replace" while we are talking about removing? In the Excel language, "remove" is just another word for "replace with an empty string" :) Luckily, we already have one, named RegExpReplace. To enable them, you need to create a custom VBA function. It is a well-known fact that out-of-the-box Excel does not support regular expressions. Knowing exactly what happens behind the scenes, it's a lot easier to work out a solution, right? How to enable regular expressions in Excel Additionally, there is the whitespace character (\s) that matches all these types and comes extremely helpful for cleaning raw input data.

Why use regular expression to trim whitespaces in Excel?īefore we dive into the nitty-gritty of using regular expressions to remove whitespaces in Excel worksheets, I'd like to address the question that comes to mind in the first place - why do we need regexes when Excel already has the TRIM function?

Replace multiple spaces with one character.Eliminate extra white space but keep line breaks.Strip extra (more than one) whitespaces.How to remove whitespace with regex - examples.How to enable regular expressions in Excel.Why use regular expression to trim whitespaces in Excel?.In some situations, however, it may become evil - extra spaces can mess up your formulas and make your worksheets almost unmanageable. In most cases, whitespace is good - you use it to visually separate different pieces of information to make it easier to perceive. Whichever input data you are using, you'll hardly encounter a dataset without spaces. Wish to handle whitespaces in the most effective way? Use regular expressions to remove all spaces in a cell, replace multiple spaces with a single character, trim spaces between numbers only, and more.
