C # Summary of regular expression options

Source: Internet
Author: User

In. when using regular expressions in. net, some options will be of great help. The options are generally represented by a letter, when using the RegEx class, we can use enumeration or input in the constructor, as shown below:

 
RegEx r =NewRegEx (@"\ W +", Regexoptions. Compiled | regexoptions. ignorecase );

You can also directly put it in front of the regular expression string in the format

(? {Option List}) {Regular Expression}

For example '(? I) I in ABC 'indicates that the case is ignored, which corresponds to the enumerated regexoptions. ignorecase.

The following describes several common options:

1. Single Row mode: N, regexoptions. singleline
This indicates that the line break can be matched, which is generally used for full-text match. You can make the regular expression match multiple lines of text.
2. Ignore case sensitivity: I, regexoptions. ignorecase
This does not need to be explained much, for example '(? I) abc' can criticize both ABC and ABC.
3. Only group match: N, regexoptions. explicitcapture
Matching unnecessary content will affect performance. We generally use (? : {Expression}) Ignore matching, But if you write too much, it will affect the readability of the regular expression,
Use the N option to make the regular expression match only the group, that is, the form (? <Name> {expression.
4. Ignore spaces and support multi-line Regular Expressions and comments: X, regexoptions. ignorepatternwhitespace
With this option, the blank space in the expression will be ignored. We can only use \ s * or \ s + to match spaces. At the same time, the expression can write multiple rows,
Supports comments starting with #, for example:
 

 
RegEx r =NewRegEx (@"(? Six)
\ W + # Name
:
\ W + # Value
");

This option is generally not used, because if it is used, we must replace all spaces in the regular expression with \ s or \ s +, which is troublesome. However, when regular expressions are very complex
Splitting multiple rows and adding comments can enhance the readability of expressions.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.