Regular Expression example

Source: Internet
Author: User
Using System;
Using System. Collections. Generic;
Using System. componentmodel;
Using System. Data;
Using System. drawing;
Using System. LINQ;
Using System. text;
Using System. Text. regularexpressions;
Using System. Windows. forms;

namespace win. regx
{< br> Public partial class frmimage: form
{< br> Public frmimage ()
{< br> initializecomponent ();
}

Private VoidBtn_click (ObjectSender, eventargs E)
{

String Str =   @"



" ;
Method1 (STR );
Method2 (STR );
Method3 ();
}

///   <Summary>
/// Method 1
/// Search for the image name in the specified string
///   </Summary>
Private   Void Method1 ( String Str)
{
// RegEx Ss = new RegEx (@"(? <= Uploadfile/mail /).*? (? = \ S *? ['""]) ");
RegEx SS =   New RegEx ( @" (? <=/) [A-zA-Z0-9] * \. (JPG | GIF | PNG )(? = \ S *? ['"]) " );
// (? <= Uploadfile/mail/) indicates reverse pre-search. Before this position, it is uploadfile/mail/
// (? = \ S *? ['""]), Indicating forward pre-search. After this position, it is \ s *? ['"]
Matchcollection MC = SS. Matches (STR );
For ( Int I =   0 ; I < MC. Count; I ++ )
{
TXT. appendtext (MC [I]. Value +   " \ R \ n " );
}
TXT. appendtext ( " \ R \ n " );
}

///   <Summary>
/// Method 2
/// Search for the image name in the specified string
///   </Summary>
Private   Void Method2 ( String Str)
{
// String Pattern = @"(? I) [^ '"] +) (['"]) [^>] *> ";
// String Pattern = @"(? I) [^ '"] +) \ 1 [^>] *> "; // \ 1 indicates the previous (['""]) match, which is more accurate.
String Pattern =   @" [^ '""] +) \ 1 [^>] *> " ; // \ 1 indicates the previous (['""]) match, which is more accurate.
RegEx Reg =   New RegEx (pattern );
Foreach (Match m In Reg. Matches (STR ))
{
TXT. appendtext (M. Groups [ " IMG " ]. Value +   " \ R \ n " );
}
TXT. appendtext ( " \ R \ n " );
}

Private   Void Method3 ()
{
// (? = Xxx), forward pre-search to determine whether the specified expression can be matched on the right of the current position
// Output 1 2
String Reg =   @" (\ D + )(? = [^ 1-9]) " ;
String Test =   " 1t2t5 " ;
Matchcollection MC = RegEx. Matches (test, Reg );
For ( Int I =   0 ; I < MC. Count; I ++ )
{
TXT. appendtext (MC [I]. Value +   " \ R \ n " );
}
TXT. appendtext ( " \ R \ n " );

// (?! (Xxx), the forward pre-search is negative, and the right side of the current position cannot match the specified expression.
// Output 5
Reg =   @" (\ D + )(?! [^ 1-9]) " ;
Test =   " 1t2t5 " ;
MC = RegEx. Matches (test, Reg );
For ( Int I =   0 ; I < MC. Count; I ++ )
{
TXT. appendtext (MC [I]. Value +   " \ R \ n " );
}
TXT. appendtext ( " \ R \ n " );

// (? <= Xxx), reverse pre-search to determine whether the left side of the current position can match the specified expression
// Output 2 5
Reg =   @" (? <= [^ 1-9]) (\ D +) " ;
Test =   " 1t2t5 " ;
MC = RegEx. Matches (test, Reg );
For ( Int I =   0 ; I < MC. Count; I ++ )
{
TXT. appendtext (MC [I]. Value +   " \ R \ n " );
}
TXT. appendtext ( " \ R \ n " );

// (? <! XXX). The reverse pre-search method is used to determine whether the specified expression can be matched on the left of the current position.
// Output 1
Reg =   @" (? <! [^ 1-9]) (\ D +) " ;
Test =   " 1t2t5 " ;
MC = RegEx. Matches (test, Reg );
For ( Int I =   0 ; I < MC. Count; I ++ )
{
TXT. appendtext (MC [I]. Value +   " \ R \ n " );
}
}
}
}

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.