Several difficulties in Regular Expressions)

Source: Internet
Author: User
Several 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.

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 the 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 URL 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

Retrieve <TD>... </TD> 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

String test = "AA <BBB> CC <DDD> ee" when a capture group exists ";
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

Delegation is rarely used in regular expressions, but sometimes it is very elegant to use delegation to solve a certain type of problem. So far, I have met twice when I use delegation, that is to process the sub-string that meets a certain condition, 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 crtprivate 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/5533/5533722.xml? Temp =. 3884394.

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.