mazamorac / ExcelMazRegex

Excel regular expressions add-in using ExcelDNA 1.0
MIT License
7 stars 1 forks source link

ExcelMazRegex

Excel regular expression add-in using .NET regex engine and ExcelDNA integration.

Version History:

Overview

ExcelMAZRegez is a simple, fast .NET regular expression library for Excel. As of v1 it's only for Excel formulas inside a worksheet; a later version might implement it for use inside VBA.

It's several orders of magnitude faster than using the VBA scripting library, and it's less convoluted than the few other Excel .NET regex libraries I found out there.

See the .Net regular expression documentation for a full description of regexes, including search and replacement patterns, and how options work.

The formula use and syntax show up in the Excel Intellisense UI, and below for reference:

Installation:

Documentation:

Function RegexEscape()

RegexEscape( text )

Return the input text with special characters escaped. Useful to construct regex patterns with arbitrary text that will not be interpreted for special pattern interpretations, such as when a string includes "[", "$", etc.

Function RegexMatch()

RegexMatch( input, pattern [, options [, replacement ] ] )

Finds and returns the text of the first instance of the regular expression pattern inside the input string, optionally modified with the option flags, and optionally with a replacement pattern.

Parameters

The options are bit flags (see below), and sould be added up to specify more than one optoin. E.g.: ignore case plus multilines is 3 (1 + 2).

if not specified, the replacement patterns defaults to "$0".

Returns:

Function RegexMatches()

RegexMatches( input, pattern [, options [, replacement ] ] )

Finds all the occurrences of the pattern in the input. Returns delimiter-separated list of matches with optional replacement pattern.

Parameters

Returns:

Function IsRegexMatch()

IsRegexMatch( input, pattern [, options ] )

Parameters

Returns:

TRUE if the pattern is found in the input, FALSE otherwise.

Function RegexMatchGroups()

RegexMatchGroups( input, pattern [, options [, MaxMatches [, MaxGroups [, IncludeDuplicates ] ] ] ] )

Search the input for matches of the pattern, return a comma delimited list of matching capture group names/numbers in match order.

Useful to find out what chunks of a regular expression were matched against, without actually caring what the text that matched was. I personally use it a lot to label data, see the examples below.

Parameters:

Notes:

Examples:

The formula:

To only return the first match, you'd use:

To return all matches but not repeat group names, you set IncludeDuplicates=FALSE:

The MaxGroups sets the max number of groups per match. Handy, for example, when different subexpressions may match the same text, and you only care for the first group that does. For example, see the difference between not using MaxGroups:

... and setting MaxGroups=1:

Function RegexGroupMatches()

RegexMatchGroups( input, pattern [, options [, MaxMatches [, MaxGroups [, IncludeDuplicates [, GroupNamesTransformPattern [, GroupNamesTransformReplacement ] ] ] ] ] ] )

Search the input for matches of the pattern, return a comma delimited list of matching capture group names/numbers in capture group order within the pattern.

The difference between RegexMatchGroups() and RegexGroupMatches() is that the first reports the results in the order of matches in the input, while the latter reports the results in the order of the search pattern. Besides, as a convenience, it allows to do a match/replace on the returned group names.

Parameters

Notes

Examples

To Do: Include examples from CalIns project showing rule precedence

Function RegexReplace()

RegexReplace( input, pattern [, options [, replacement ] ] )

Finds all the instances of the search pattern in the input text, optionally modified with the option flags, replaces them with the replacement pattern, and returns the modifed input. Similar to RegexMatch() but searching and replacing the entire input, and the replacement pattern default is an empty string ("").

The parameters, and options for RegexReplace() are the same as for RegexMatch(), except for the replacement default.

Returns:

General notes: