又是正则表达式
我想匹配<table width="100%" height="" border="0" style=""
cellpadding="0" cellspacing="0">
这里面的每一个属性都是可省的,应该怎么写啊?
问题点数:100、回复次数:6Top
1 楼saucer(思归)回复于 2003-08-02 02:25:40 得分 0
if you don't need to match those attributes, you can do
<table[^>]*>
Top
2 楼shixueli(我们总是不能随心所欲)回复于 2003-08-02 02:33:24 得分 0
to saucer(思归, MS .NET MVP):
我是想
除了我上面写的属性之外,如果再有别的东西,比如onclick我是不会匹配的,
怎么办,你的方法连onclik都匹配了Top
3 楼shixueli(我们总是不能随心所欲)回复于 2003-08-02 02:44:07 得分 0
帮帮忙吧,本人的另外一个贴的分也可以给他:
http://expert.csdn.net/Expert/topic/2097/2097696.xml?temp=.5310938
同一个问题Top
4 楼saucer(思归)回复于 2003-08-02 02:52:41 得分 100
if you want to match everything, try
string[] slist = {"<table width=\"100%\" height=\"\" border=\"0\" style=\"\" cellpadding=\"0\" cellspacing=\"0\">",
"<table>","<table border=\"1\" >", "<table border=\"1\" width=\"100%\" test=\"ss\" >",
"<table test=\"ss\" border=\"1\" width=\"100%\" >"
}
;
Regex re = new Regex(@"<table(?:[^>]*?(?:width=""(?<width>[^""]*)""|height=""(?<height>[^""]*)""|border=""(?<border>[^""]*)""|style=""(?<style>[^""]*)""|cellpadding=""(?<cellpadding>[^""]*)""|cellspacing=""(?<cellspacing>[^""]*)""))*[^>]*?>",
RegexOptions.IgnoreCase | RegexOptions.Singleline);
foreach (string s in slist)
{
Console.WriteLine("\nfor:\t{0}\n",s);
Match m = re.Match(s);
if (m.Success)
{
Console.WriteLine("width:{0}",m.Groups["width"].Value);
Console.WriteLine("height:{0}",m.Groups["height"].Value);
Console.WriteLine("border:{0}",m.Groups["border"].Value);
Console.WriteLine("style:{0}",m.Groups["style"].Value);
Console.WriteLine("cellpadding:{0}",m.Groups["cellpadding"].Value);
Console.WriteLine("cellspacing:{0}",m.Groups["cellspacing"].Value);
}
}
if you just want what you listed it, try
string[] slist = {"<table width=\"100%\" height=\"\" border=\"0\" style=\"\" cellpadding=\"0\" cellspacing=\"0\">",
"<table>","<table border=\"1\" >", "<table border=\"1\" width=\"100%\" test=\"ss\" >",
"<table test=\"ss\" border=\"1\" width=\"100%\" >"
}
;
Regex re = new Regex(@"<table(?:\s*(?:width=""(?<width>[^""]*)""|height=""(?<height>[^""]*)""|border=""(?<border>[^""]*)""|style=""(?<style>[^""]*)""|cellpadding=""(?<cellpadding>[^""]*)""|cellspacing=""(?<cellspacing>[^""]*)""))*\s*>", RegexOptions.IgnoreCase | RegexOptions.Singleline);
foreach (string s in slist)
{
Console.WriteLine("\nfor:\t{0}\n",s);
Match m = re.Match(s);
if (m.Success)
{
Console.WriteLine("width:{0}",m.Groups["width"].Value);
Console.WriteLine("height:{0}",m.Groups["height"].Value);
Console.WriteLine("border:{0}",m.Groups["border"].Value);
Console.WriteLine("style:{0}",m.Groups["style"].Value);
Console.WriteLine("cellpadding:{0}",m.Groups["cellpadding"].Value);
Console.WriteLine("cellspacing:{0}",m.Groups["cellspacing"].Value);
}
}Top
5 楼shixueli(我们总是不能随心所欲)回复于 2003-08-02 02:55:51 得分 0
试试Top
6 楼shixueli(我们总是不能随心所欲)回复于 2003-08-02 03:13:58 得分 0
好用,谢谢思归大哥!Top




