Refinance now before rates go up! Get multiple rate quotes at GetMyLender.com.

Regular Expression in Dot Net Framework Part V - Regular Expression Options and Miscellaneous Constructs

A regular expression is a pattern that the regular expression engine attempts to match in input text. A pattern consists of one or more character literals, operators, or constructs.


Regular Expression Options

You can specify options that control how the regular expression engine interprets a regular expression pattern. Many of these options can be specified either inline (in the regular expression pattern) or as one or more RegexOptions constants. This quick reference lists only inline options. For more information about inline and RegexOptions options, see the article Regular Expression Options.

You can specify an inline option in two ways:

  • By using the miscellaneous construct (?imnsx-imnsx), where a minus sign (-) before an option or set of options turns those options off. For example, (?i-mn) turns case-insensitive matching (i) on, turns multiline mode (m) off, and turns unnamed group captures (n) off. The option applies to the regular expression pattern from the point at which the option is defined, and is effective either to the end of the pattern or to the point where another construct reverses the option.

  • By using the grouping construct (?imnsx-imnsx:subexpression), which defines options for the specified group only.

The .NET Framework regular expression engine supports the following inline options.

Option Description Pattern Matches
i Use case-insensitive matching. \b(?i)a(?-i)a\w+\b "aardvark", "aaaAuto" in "aardvark AAAuto aaaAuto Adam breakfast"
m Use multiline mode. ^ and $ match the beginning and end of a line, instead of the beginning and end of a string. For an example, see the "Multiline Mode" section in Regular Expression Options.
n Do not capture unnamed groups. For an example, see the "Explicit Captures Only" section in Regular Expression Options.
s Use single-line mode. For an example, see the "Single-line Mode" section in Regular Expression Options.
x Ignore unescaped white space in the regular expression pattern. \b(?x) \d+ \s \w+ "1 aardvark", "2 cats" in "1 aardvark 2 cats IV centurions"

Miscellaneous Constructs

Miscellaneous constructs either modify a regular expression pattern or provide information about it. The following table lists the miscellaneous constructs supported by the .NET Framework.

Construct Definition Example
(?imnsx-imnsx) Sets or disables options such as case insensitivity in the middle of a pattern. \bA(?i)b\w+\b matches "ABA", "Able" in "ABA Able Act"
(?# comment) Inline comment. The comment ends at the first closing parenthesis. \bA(?#Matches words starting with A)\w+\b
# [to end of line] X-mode comment. The comment starts at an unescaped # and continues to the end of the line. (?x)\bA\w+\b#Matches words starting with A

No comments:

Post a Comment