RegExMatch()
Searches for a pattern in a string using a regular expression.
A regular expression is a sequence of characters used for matching patterns in a string of text.
The RegExMatch()
function has four parameters:
sourceString
(string): Required. The string to search using regular expression.regExPattern
(string): Required. The regular expression to use in the search.returnValue
(string): Required. The name or ordinal of the matching group to return.repeatParameter
(string): The repeating string parameter to apply. You can use any value from the .NET RegexOptions enumeration, such as IgnoreCase and Multiline.
To use the function, pass it a string, a regular expression to apply to the string, and the value from the regular expression that you want to return.
This example uses a regular expression to determine if it contains 5–7 alphanumeric characters.
The example outputs a message indicating that the string matched the pattern in the regular expression.
You can combine the RegExMatch()
function with the Replace() function to replace text in a string based on a regex pattern. This example uses the RegExMatch()
function to find name prefixes such as Mr. and Mrs. It then uses the Replace()
function to remove those prefixes from each name in a rowset. You could perform a similar operation by using the ReplaceList() function. However, the ReplaceList()
function can only replace static strings. By using regular expressions, you can handle irregularities in the source data. For example, the regular expression used in this example works the same if there are multiple spaces after the prefix, or if the prefix isn’t followed by a period.
The code outputs a list of names with the prefixes removed.
- Microsoft Learn: .NET Regular Expressions
- RegularExpressions.info: Regular Expressions Tutorial