C # Regular Expression Learning (2)

Source: Internet
Author: User
Iii. Difficulties in Regular Expressions
1. Greedy and non-greedy
Post that helps you understand this concept

Http://community.csdn.net/Expert/topic/5435/5435240.xml? Temp =. 7199671.

 

Note:

When talking about greed and non-Greed, one thing that everyone may ignore is a separate "?", It indicates the greedy mode, and "?" Is not greedy.
Example:
Source string: 12
Regular Expression (1): [1-9]/D?
Regular Expression (2): [1-9]/D ??
Result: (1) the matching is successful, the matching result is 12, (2) the matching is successful, and the matching result is 1.

2. forward and reverse pre-Search
Post that helps you understand this concept

Http://community.csdn.net/Expert/topic/5410/5410564.xml? Temp =. 1138422.

 
Iv. Common Regular Expressions 1. Verify the regularexpressionvalidator Control
Note:

1. regularexpressionvalidator cannot be used to verify non-empty fields. requiredfieldvalidator is used to verify non-empty fields.

2. If there are no special circumstances, "^" and "$" must be added before and after the regular expression used to verify the control, because the content verified by the verification control must be completely matched, instead of successfully matching some of them, you can pass verification. 3. It is used to verify the regular expression of the Control. Finally, it must be converted to a client script. Because the script language has weak support for regular expressions, some regular expressions that can be passed in the CS program may be invalid in the verification control.   Post that helps you understand this concept

Http://community.csdn.net/Expert/topic/5445/5445334.xml? Temp =. 3749658.

 


2. Matching
Matching is to check whether the string meets a certain rule or whether it contains a sub-string that meets a certain rule.
RegEx. ismatch
Example

Determine whether the input content of textbox1 is in yyyy-mm format

String yourstr = textbox1.text;
If (RegEx. ismatch (yourstr, @ "^/d {4}-(0/d | 1 [0-2]) $ "))
{
MessageBox. Show ("compliant ");
}
Else
{
MessageBox. Show ("not compliant ");
}

3. Extraction
Extraction is to extract the sub-string matching a certain rule from the string. Match m = RegEx. match matchcollection MC = RegEx. matches is used to retrieve a single matching result, while matches is used to retrieve Multiple matching results and store them into a set.

Example

Retrieve a single image address

String yourstr = " ";
String resultstr = "";
Match m = RegEx. Match (yourstr, @ "] *? Src = (["" ']?) (? <Source> [^ "" '/S] *)/1 [^>] *?> ", Regexoptions. ignorecase );
If (M. Success)
{
Resultstr = M. Groups ["Source"]. value;
}

Retrieve multiple image addresses

String yourstr = "<p> </P> <p> & nbsp; </P> <p> </P> <p> & nbsp; </P> ";
Matchcollection MC = RegEx. Matches (yourstr, @ "] *? Src = (["" ']?) (? <SRC> [^ "" '/S] *)/1 [^>] *?> ", Regexoptions. ignorecase );
Foreach (Match m in MC)
{
Richtextbox2.text + = M. Groups ["src"]. Value + "/N ";
} Post that helps you understand this concept

Http://community.csdn.net/Expert/topic/5479/5479400.xml? Temp =. 2616083.

Http://community.csdn.net/Expert/topic/5441/5441044.xml? Temp =. 3489954. 4. Replacement
Replacement is to replace one form of string with another form, or delete unnecessary content RegEx. Replace

Example

Convert UBB code to HTML Format

String test = "[img] http://www.csdn.net/logo.jpg#/img]";
String resultstr = RegEx. Replace (test, @ "/[img/] ([^/] *?) /[/Img/] ", @" ");

Output:

Example

Remove the non-HTML code section in the <TD>... </TD> label.

String test = "<TD class = btd width = 198 bgcolor =/" # fcfcfc/"> <B> <a href =/" http://dict.pconline.com.cn/dic/sort.jsp? Kindid =-1 & dicid = 3142/"target =/" _ blank/"> display type </a> </B> </TD> ";
String resultstr = RegEx. Replace (test, @ "<[^>] *?> "," "). Trim ();

Output: display type

5. Segmentation
RegEx. Split Note:

When a capture group is used in split, the split array also contains the content of the capture group. Even if a zero-width capture group is used, the content of the capture group is saved to the result array.

Example

When a capture group does not exist

String test = "AA <BBB> CC <DDD> ee ";
String [] temp = RegEx. Split (test, @ "<[^>] *?> ");
Foreach (string s in temp)
{
Richtextbox2.text + = S + "/N ";
}

Output:
AA
CC
EE

When a capture group exists

String test = "AA <BBB> CC <DDD> ee ";
String [] temp = RegEx. Split (test, @ "(<[^>] *?>) ");
Foreach (string s in temp)
{
Richtextbox2.text + = S + "/N ";
}

Output:
AA
<BBB>
CC
<DDD>
EE

Post that helps you understand this concept

Http://community.csdn.net/Expert/topic/5436/5436187.xml? Temp =. 7995264.

6. Delegate

In regular expressions, delegation can solve a type of problem elegantly or improve efficiency. Delegation is generally used to process substrings that meet certain conditions, rather than processing all

Matchevaluator (string (MATCH) target)

Example

Only the part of the <B>... </B> label that does not contain HTML code is replaced by CRT.

Private void button2_click (Object sender, eventargs E)
{
String yourstr = "<TD class = btd width = 198 bgcolor =/" # fcfcfc/"> <B> <a href =/" http://dict.pconline.com.cn/dic/sort.jsp? Kindid =-1 & dicid = 3142/"target =/" _ blank/"> display type </a> </B> <B> wuxga + </B> </ TD> ";
String resultstr = RegEx. Replace (yourstr ,@"(? <= <B [^>] *>) [^ <>] * (? = </B>) ", new matchevaluator (expreplace), regexoptions. ignorecase );

}
Private string expreplace (Match m)
{
Return M. value. Replace (M. value, "CRT ");
}

Output: <TD class = btd width = 198 bgcolor = "# fcfcfc"> <B> <a href = "http://dict.pconline.com.cn/dic/sort.jsp? Kindid =-1 & dicid = 3142 "target =" _ blank "> display type </a> </B> <B> CRT </B> </TD>

Post that helps you understand this concept

Http://community.csdn.net/Expert/topic/5520/5520530.xml? Temp =. 769314.
Http://community.csdn.net/Expert/topic/5580/5580796.xml? Temp =. 6849176.
Http://community.csdn.net/Expert/topic/5578/5578425.xml? Temp =. 2284815.


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.