Escapes metacharacters in a string for use as literals in an expression. IsMatch Returns true if the regex finds a match in the input string. Match Returns a Match object if a match is found in the input string. Matches Returns a MatchCollection object containing any and all matches found in the input string. Replace Replaces matches in the input string with a given replacement string. Split Returns an array of strings by splitting up the input string into array elements separated by regex matches.
In addition to many methods, there are also a number of options that can be specified, usually in the constructor of the Regex object. These options are part of a bitmask, and thus can be OR'd together yes, you can have both Multiline and Singleline turned on at the same time. Option Description Compiled Use this option when you will be doing many match operations in a loop.
This saves the step of parsing the expression on each iteration. Multiline Has nothing to do with how many lines are in the input string. IgnoreCase Causes the pattern to ignore case sensitivity when matching the search string.
IgnorePatternWhitespace Allows pattern to have as much white space as desired, and also enables the use of in-pattern comments, using the? SingleLine Has nothing to do with how many lines are in the input string. Rather, will cause the. Some common things you may use regular expressions for include validating, matching, and replacing. In many cases, these can be accomplished using static methods of the Regex class, without any need to instantiate the Regex class itself.
To perform validation, all you must do is create or find the right expression and apply it to your input string using the IsMatch method of the Regex class. For example, the following function demonstrates how to use a regular expression to validate a ZIP code: Replace inputString, pattern, replacementText ; Finally, you can iterate through a collection of matches in an input string using code like this: In particular, setting options. For example, to create an instance of Regex that ignores case and pattern white space, and then retrieve the set of matches for that expression, you would use code like the following: Matches inputString ; Complete working versions of these samples are included in the download for this article, as simple ASP.
Free Tools The Regulator http: Includes performance analysis and syntax highlighting. NET code and compiled assembliles to help you integrate expressions into your applications. Regular Expression Workbench v2. Has "Examine-o-matic" feature, allowing you to hover the mouse over a regex to decode its meaning. Advanced Topics Two regular expression features that really make me have to think are named groups and lookaround processing.
Since you'll only need these on rare occasions, I'll only briefly describe these topics here. With named groups, you can name individual matching groups and then refer to these groups within the expression programmatically. This can be especially powerful when combined with the Replace method as a way of reformatting an input string by re-arranging the order and placement of the elements within the input string.
You could use write an expression to capture the first format, iterate through its Matches collection, parse each string, and use string manipulation to build the replacement string.
This would require a fair amount of code and a fair amount of processing. Using named groups, you could accomplish the same things like so: In any event such references are collectively referred to as backreferences. Another common use of backreferences is within matching expressions themselves, such as this expression for finding repeated letters: Backreferences allow an expression to remember things about parts of the input string it has already parsed and matched.
Not all regular expression engines support all variations of lookaround processing. These constructs do not consume characters even though they may match them.
Some patterns are impossible to describe without lookaround processing, especially ones in which the existence of one part of the pattern depends on the existence of a separate part. The syntax for each flavor of lookaround is described below.