2011-01-13 31 views
1

我尝试重新排列我的sorce文件,例如:从aspx页面中删除所有CSS样式。 我有一个文件(C:/文件:mypage.aspx和30-40 aspx页面)如何删除Css样式链接等在C#中使用正则表达式?我需要你的最佳帮助它是重要的:)我尝试写一个winforms(.EXE)罗马css属性如何使用Regex去除aspx页面源代码中的CSS类?

我想删除下面的一切!


    <link href="../Styles/Interm.css" rel="stylesheet" type="text/css" /> 
<style type="text/css"> 

// ERASE EVERYTHING 
    </style> 
    class="PageHeader" 
CssClass="style1" 

CssClass="MyCss",CssFilePath="/test/style.css",CssPostFix="Aqua" 

MY码:


using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Windows.Forms; 
using System.Text.RegularExpressions; 

namespace RemoverTags 
{ 
    public partial class Form1 : Form 
    { 
     public Form1() 
     { 
      InitializeComponent(); 
     } 

     private void button1_Click(object sender, EventArgs e) 
     { 
      richTextBox1.Text = RemoveWhiteSpaceFromStylesheets(richTextBox1.Text); 
     } 

     public string RemoveWhiteSpaceFromStylesheets(string body) 
     { 
      body = Regex.Replace(body, "type="+"\"text/css\"", ""); 

      body = Regex.Replace(body, @"[a-zA-Z]+#", "#"); 

      body = Regex.Replace(body, @"[\n\r]+\s*", string.Empty); 

      body = Regex.Replace(body, @"\s+", " "); 

      body = Regex.Replace(body, @"\s?([:,;{}])\s?", "$1"); 

      body = body.Replace(";}", "}"); 

      body = Regex.Replace(body, @"([\s:]0)(px|pt|%|em)", "$1"); 



      // Remove comments from CSS 

      body = Regex.Replace(body, @"/\*[\d\D]*?\*/", string.Empty); 


      Regex r1 = new Regex(@"<style >([A-Za-z0-9\-]+)</style>"); 
      Match match = r1.Match(body); 
      string v = ""; 
       if (match.Success) 
       { 
        v = match.Groups[1].Value; 

       } 
       body = Regex.Replace(body, v, string.Empty); 
       body = Regex.Replace(body, @"<(style|\n)*?>", string.Empty); 
       body = Regex.Replace(body, @"<(/style|\n)*?>", string.Empty); 
      return body; 

     } 

    } 
} 

but not working... 
+0

我正确地认为你想自动**从页面中删除CSS? – 2011-01-13 18:36:35

回答

0

使用Visual Studio查找工具(CTRL + SHIFT + F),进入 “查找选项”,然后选择正则表达式替换所有样式元素。

相关问题