How to remove CSS Class in aspx pages source codes using Regex?
i try to rearrange my sorce files eg: remove all css styles from aspx Pages. i have a file (C:/file:mypage.aspx and 30-40 aspx pages) how can i remove Css style link etc. with using Regex in C#? Please i need your best helps it is important:) I try to write a winforms(.EXE) to rome css property
i want to remove belows everything!
<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 CODES:
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...
Use Visual Studio find tool (ctrl+shift+f), go to "find options" and choose regular expression to replace all style elements.
精彩评论