C # Regular Expression Learning (1)

Source: Internet
Author: User
This article is just a bit of experience I have accumulated in the regular expression learning process. It provides a reference for beginners.

Unless otherwise stated, this article will only discuss the regular expression application in C #. Due to my limited level and a rush of time, please criticize and correct any mistakes, I will promptly correct and add some new content one after another. I recommend an article + A tool + a regular learning method Tutorial: http://www.regexlab.com/zh/regref.htm tool: Match tracer 1.2 my level is limited, the tutorial cannot be written, so we recommend the above regular tutorial. This is the best tutorial I think, at least in the basic Chinese tutorial. The best way to learn regular expressions is to do a lot of exercises. Search for a post with the keyword "regular" on csdn. Do not read the reply first. Read the requirements of the landlord and try to write it in reference to the tutorial, if you get the correct results, or if you can't do it yourself, you can check the reply. There are about a hundred or more posts, so you should have no problem with the basic application of your regular expressions. Ii. Several misunderstandings of Regular Expressions for beginners 1 ,. the decimal point can match any character other than the line break (/n). to match the decimal point itself, add a slash (/) to escape the character. Do not try to use [. /n] to match any character (. |/N), but it is generally not used in this way. It is generally used in [/S] Or. add regexoptions. the singleline parameter is used to achieve this. 2. [] square brackets [] contain a series of characters, which can match any of them. If [^] is used to contain a series of characters, it can match any character other than the characters. In the same way, although it can match any of them, it can only be one, not multiple. Remember that [] matches only one character, not multiple. The easiest mistake for beginners is to use expressions like [^ ABC] to match strings that do not contain the "ABC" substring. ExampleSource string: String yourstr = "<AAA> BBB <ABC> CCC <DDD>"; Result: <AAA>, <DDD> indicates that the value of yourstr is <...>, however, <> incorrect syntax for content that is not ABC: <[^ ABC] *> correct syntax: <(?! ABC>) [^>] *> In addition, most characters that have special meanings and need to be escaped when matching the regular expression itself do not need to be escaped in. Only "/", "[", and "]" must be escaped, while "^" appears at the starting position of []. When "-" is used to form a range, it must be escaped, escape is not required for other locations, such as [/^. $ ^ {/[(|) * +? //-] 3. W any letter, number, or underline, that is, ~ Z, ~ Z, 0 ~ In general, this is the case. However, in some cases,/W matches the local character set, such as the Chinese character of the Chinese system. Therefore, the explicit requirement is ~ Z, ~ Z, 0 ~ 9, _ in a time, with [A-Za-z0-9 _], instead of/W 4,/escape some special meaning in the regular expression characters, such as to match these characters themselves, escape it. Refer to MS-help: // Ms. msdnqtr. v80.chs/MS. msdn. v80/MS. visual Studio. v80.chs/dv_fxfund/html/f49cc9cc-db7d-4058-8b8a-422bc08b29b0.htm in practical application, according to the specific situation, the characters to be escaped may not exceed the following listed characters. $ ^ {[(|) * +? /In addition,/s,/B,/W,/d, and other special characters in the regular expression, which must be escaped twice when used in the string, that is, string pattern = "The // B // W + // B number is // D"; or add "@" to the front, unescape string pattern = @ "The/B/W +/B number is/D"; Special: "", used in a string, escape with/", used in strings, escape string pattern = "with" " "; string Pattern = @ " Example

(1), ^/d + /. /d |/d + $ (2), ^ (/d + /. /d) | (/d +) $ (3), ^ (/d + /. /d |/d +) $ (1) and (2) have the same effect, indicating "|" the relationship between the two sides is "or, ^/d + /. the result of/D or/d + $ and (3) is different from the preceding two because it indicates/d + /. the relationship between/d and/d + is "or". This expression is equivalent to ^/d + /. /d $ | ^/d + $ For example: 123.45 (1) and (2) are matched successfully, and the matching result is 123.4 (3). Because of this Sub-requirement, it can be a number without a decimal point, or a maximum of one decimal point. When there are multiple "|", the matching order of the range "|" is also divided from left to right. If the left side does not meet the requirements, try to match the right expression, if the matching on the left side is successful, the matching on the right side will not be tried again. If there are multiple "|" expressions, try matching from left to right. By the way, [] implies the "or" relationship. It is wrong to use "|" in [] to indicate "or, in this case, only one common character "|" is added to match the character "|". () is the same.
Post that helps you understand this concept

Http://community.csdn.net/Expert/topic/5474/5474473.xml? Temp =. 9890863.

6. regexoptions. singleline | regexoptions. some new users of Multiline simply think that these two attributes are mutually exclusive. In fact, they are used to change the meaning of a specific symbol in a regular expression.

Regexoptions. singleline specifies the single row mode. Change the meaning of a vertex (.) so that it matches each character (instead of each character except/N ). Regexoptions. multiline mode. Change the meaning of ^ and $ so that they match the beginning and end of a row, not just the beginning and end of the entire string.

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.