Asp.net regular template engine code, asp.net template engine

Source: Internet
Author: User

Asp.net regular template engine code, asp.net template engine

We declare an array

   public static Regex[] r = new Regex[23];

The following key Regular Expressions:

RegexOptions options = RegexOptions. None; // nested template tag (compatible) r [0] = new Regex (@ "<! -- {Template (skin = \ "" ([^ \ [\] \ {\} \ s] + )\\""(?: \ S + ))?) Src = (?: \/| \ "") ([^ \ [\] \ {\}\ S] + )(?: \/| \\"")(?: \ S *)} --> ", options); // template path tag (new) r [1] = new Regex (@" <! -- {Templateskin (= (?: \ "") ([^ \ [\] \{\}\ S] + )(? :\\""))?) (?: \ S *)} --> ", options); // namespace tag r [2] = new Regex (@" <! -- {Namespace (? :""?) ([\ S \ S] + ?) (? :""?)} --> ", Options); // C # code tag r [3] = new Regex (@" <! -- {Csharp} --> ([\ s \ S] + ?) <! -- {/Csharp} --> ", options); // loop (discard) r [4] = new Regex (@" <! -- {Loop (\ ([^ \ [\] \{\}\ s] + )\))?) ([^ \ [\] \{\}\ S] +) ([^ \ [\] \{\}\ s] +)} --> ", options ); // foreach loop (new) r [5] = new Regex (@ "<! -- {Foreach (?: \ S *) \ ([^ \ [\] \{\}\ s] +) ([^ \ [\] \{\}\ s] +) in ([^ \ [\] \{\}\ s] + )\)(?: \ S *)} --> ", options); // for Loop (new) r [6] = new Regex (@" <! -- {For \ ([^ \ (\) \ [\] \ {\}] + )\)(?: \ S *)} --> ", options); // if statement label (discard) r [7] = new Regex (@" <! -- {If (?: \ S *) ([^ \ s] + )((?: \ S *) (\ | \&\&)(?: \ S *) ([^ \ s] + ))?) (?: \ S *)} --> ", options); r [8] = new Regex (@" <! -- {Else (((?: \ S *) if (?: \ S *) ([^ \ s] + )((?: \ S *) (\ | \&\&)(?: \ S *) ([^ \ s] + ))*))?) (?: \ S *)} --> ", options); // if statement label (new) r [9] = new Regex (@" <! -- {If \ ([^ \ s] + )((?: \ S *) (\ | \&\&)(?: \ S *) ([^ \ s] + ))?) \)(?: \ S *)} --> ", options); r [10] = new Regex (@" <! -- {Else (((?: \ S *) if \ ([^ \ s] + )((?: \ S *) (\ | \&\&)(?: \ S *) ([^ \ s] + ))?))? \))(?: \ S *)} --> ", options); // loop and Judge end tag (compatible) r [11] = new Regex (@" <! --{\/(?: Loop | foreach | for | if )(?: \ S *)} --> ", options); // continue tag r [12] = new Regex (@" <! -- {Continue (?: \ S *)} --> "); // break tag r [13] = new Regex (@" <! -- {Break (?: \ S *)} --> "); // request tag r [14] = new Regex (@ "(\ {request \ [([^ \ [\] \ {\} \ s] +) \] \}) ", options); // capture the string tag r [15] = new Regex (@" (<! -- {Cutstring \ ([^ \ s] + ?), (. \ D *?) \)} -->) ", Options); // url link tag r [16] = new Regex (@" (<! -- {Linkurl \ ([^ \ s] *?) \)} -->) ", Options); // declare the value assignment tag (compatible) r [17] = new Regex (@" <! -- {Set ((\(? ([\ W \. <>] + )(?: \) | ))?) (?: \ S *)\{? ([^ \ S \ {\}] + )\}? (?: \ S *) = (?: \ S *)(.*?) (?: \ S *)} --> ", options ); // data variable label r [18] = new Regex (@ "(\ {([^ \ [\] \ {\} \ s] +) \ [([^ \ [\] \ {\}\ s] +) \] \}) ", options ); // common variable label r [19] = new Regex (@ "({([^ \ [\]/\{\}=: '\ s] + )}) ", options); // time format conversion tag r [20] = new Regex (@" (<! -- {Datetostr \ ([^ \ s] + ?), (.*?) \)} -->) ", Options ); // Integer Conversion label r [21] = new Regex (@ "(\ {strtoint \ ([^ \ s] + ?) \) \}) ", Options); // directly output the tag r [22] = new Regex (@" <! --{(?: Write | = )(?: \ S *)(.*?) (?: \ S *)} --> ", options );

Looking at a bunch of things! It is difficult to get regular expressions.

How can we use the following method to replace the if tag of the judgment statement?

String strTemplate = "" // the content of the template you want to replace foreach (Match m in r [7]. matches (strTemplate) {IsCodeLine = true; strTemplate = strTemplate. replace (m. groups [0]. toString (), "\ r \ n \ tif (" + m. groups [1]. toString (). replace ("\" "," \ "") + ") \ r \ n \ t {");} foreach (Match m in r [8]. matches (strTemplate) {IsCodeLine = true; if (m. groups [1]. toString () = string. empty) {strTemplate = strTemplate. replace (m. groups [0]. toString (), "\ r \ n \ t} \ r \ n \ telse \ r \ n \ t {");} else {strTemplate = strTemplate. replace (m. groups [0]. toString (), "\ r \ n \ t} \ r \ n \ telse if (" + m. groups [3]. toString (). replace ("\" "," \ "") + ") \ r \ n \ t {") ;}} foreach (Match m in r [9]. matches (strTemplate) {IsCodeLine = true; strTemplate = strTemplate. replace (m. groups [0]. toString (), "\ r \ n \ tif (" + m. groups [1]. toString (). replace ("\" "," \ "") + ") \ r \ n \ t {");} foreach (Match m in r [10]. matches (strTemplate) {IsCodeLine = true; if (m. groups [1]. toString () = string. empty) {strTemplate = strTemplate. replace (m. groups [0]. toString (), "\ r \ n \ t} \ r \ n \ telse \ r \ n \ t {");} else {strTemplate = strTemplate. replace (m. groups [0]. toString (), "\ r \ n \ t} \ r \ n \ telse if (" + m. groups [3]. toString (). replace ("\" "," \ "") + ") \ r \ n \ t {");}}

Writing a template engine by yourself is troublesome. It may be easier to directly write dynamic pages and pseudo static pages. Previously, we used the velocity template engine, which is also very good.


The aspnet Regular Expression loads the content in the <body> label in the html source code.

Please refer to the following code
The url is a webpage address.
Var url = textBox1.Text;
Var wc = new WebClient ();
Var html = wc. DownloadString (url );
RichTextBox1.Text = Regex. Match (html, "<body. + </body>"). ToString ();



ASPnet uses regular expressions to extract img tags of html code

Regex r = new Regex ("] + [>] +", RegexOptions. ExplicitCapture );
String result = r. Replace (strSource ,"");

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.