C # RegEx Regular Expression matching all characters (subject to the newline carriage return \ r \ n problem)

Source: Internet
Author: User

Let's just talk about the content.

Use RegEx. when using the match () method, you want to match a substring, but the content to be matched may contain line breaks in the resources I load, for example, if the content of my resource is John \ r \ n Smith, I want to match John Smith.

1. vertices (.) in a regular expression (.) represents all characters except \ n, but this is only used outside brackets ([]). If used in brackets, it only represents the dot itself, so this regular expression can be written as "John (. | \ n )*? Smith ".

2. in a regular expression, \ s indicates all blank characters including tabulation (\ t), line feed, and space. \ s indicates all non-blank characters including tabulation, line feed, and space, then the two free combinations can get all the characters, so you can write "John [\ s] *? Smith ",

3. C # RegEx. the match () method receives 3rd parameters. This parameter is a regexoptions enumeration. After my experiment, I found that when I want to match the content of line breaks and carriage returns, set 3rd parameters to regexoptions. singleline. You can write "John. *? Smith ", the same can be matched successfully. So this is also a method. The guess is to enhance the matching effect of the dot number, for example, matching \ n (of course, it is only one kind of guess, you can add an independent \ n to the string, not \ r, and match the result)

 

Note: parentheses are used in 1, or joined, and brackets are used in 2. Regular Expressions in brackets are automatically selected.

 

\ R press ENTERCode = 13Return (the mental retardation is to move the cursor to the starting position of the line, that is, the Y coordinate remains unchanged)

\ N line feedCode = 10Newline)

In Windows OS, the Enter key on the keyboard performs the \ r \ n operation, but only the \ n operation in Linux. Therefore, pay attention to this in the code.

 

 

 

 

Conclusion

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.