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 " );
}
}
}
}