FAQ: How do I capture strings that include special characters using regex?

Question: I need to capture customer names that have a slash, comma, and asterisk in the line.

Example:

Originator:/Account:*******1000,Payor’s Business,100 Main St,

The above example has a forwardslash that must be included in the capture value, but a forward slash is a regex special character.

Answer: Regular expressions assign functionality to the following characters:

[\^$.|?*+()/

When applying regex to a string field that includes one of these characters in its value, you must escape the special functionality regex engines assign to the character by preceding it with a backslash (\). Regex engines interpret a backslash before any special character as its literal.

Most regex engines interpret a forward slash as the beginning or end of a regular expression.

Example:

Originator: [A-Z- *\/]{0,20}[0-9,]{0,20}([A-Z ]{0,30})

This part of the expression:

[A-Z- *\/]

catches any capital letter followed by a hyphen and a space followed by any character including the forward slash. Notice the backslash before the forward slash escapes the special character regex functionality as interpreted by a regex engine. The rest of the expression defines the parameters for the series of numbers to follow.