var list = Regex.Matches(tempStr, @"(?is)<div[^>]*?style=(['""]?)(z-index:\s*?(?<zIndex>-?\d+)|left:\s*?(?<left>-?\d+(.\d+)?)|top:\s*?(?<top>-?\d+(.\d+)?)|font-size:\s*?(?<fontSize>\d+)|[^'""])*?\1[^>]*?>(?<text>[^<>]*?)</div>").OfType<Match>().Select(t => new { zIndex = t.Groups["zIndex"].Value, left = t.Groups["left"].Value, top = t.Groups["top"].Value, fontSize = t.Groups["fontSize"].Value, text = t.Groups["text"].Value }).ToList(); /* + [0] { zIndex = "101", left = "23.17", top = "23.1", fontSize = "20", text = "日期(Date):2012年12月1日" } <Anonymous Type> */
Regex r = new Regex("^\\<[^\\<|\\>]+style=['|\"](?<style>[^'|\"]+)['|\"]\\>(?<center>[^\\<|\\>]+)(?<right>\\</[^\\<|\\>]+\\>)$"); string str = @"<div style='Z-INDEX:101;position:absolute;left:23.17mm;top:23.1mm;font-size:20;font-family:黑体;BACKGROUND-COLOR: white; color: black'>日期(Date):2012年12月1日</div>"; Match m = r.Match(str); Console.WriteLine(m.Groups["center"].Value); r = new Regex("(?<name>[^'|\"|:]+):(?<value>[^'|\"|;]+);"); str = m.Groups["style"].Value; MatchCollection mc = r.Matches(str); foreach (Match match in mc) { Console.WriteLine("name:{0}\tvalue:{1}", match.Groups["name"].Value, match.Groups["value"]); } Console.ReadKey();
var list = Regex.Matches(tempStr, @"(?is)<div[^>]*?style=(['""]?)(z-index:\s*?(?<zIndex>-?\d+)|left:\s*?(?<left>-?\d+(\.\d+)?)|top:\s*?(?<top>-?\d+(\.\d+)?)|font-size:\s*?(?<fontSize>\d+)|[^'""])*?\1[^>]*?>(?<text>[\s\S]*?)</div>").OfType<Match>().Select(t => new { zIndex = t.Groups["zIndex"].Value, left = t.Groups["left"].Value, top = t.Groups["top"].Value, fontSize = t.Groups["fontSize"].Value, text = t.Groups["text"].Value }).ToList();
var list = Regex.Matches(tempStr, @"(?is)<div[^>]*?style=(['""]?)(z-index:\s*?(?<zIndex>-?\d+)|left:\s*?(?<left>-?\d+(\.\d+)?)|top:\s*?(?<top>-?\d+(\.\d+)?)|font-size:\s*?(?<fontSize>\d+)|[^'""])*?\1[^>]*?>\s*?(<b>)?(?<text>((?!<img[^>]*?>)[\s\S])*?)\s*?(</b>)?</div>").OfType<Match>().Select(t => new { zIndex = t.Groups["zIndex"].Value, left = t.Groups["left"].Value, top = t.Groups["top"].Value, fontSize = t.Groups["fontSize"].Value, text = t.Groups["text"].Value }).ToList();
var list = Regex.Matches(tempStr, @"(?is)<div[^>]*?style=(['""]?)(z-index:\s*?(?<zIndex>-?\d+)|left:\s*?(?<left>-?\d+(\.\d+)?)|top:\s*?(?<top>-?\d+(\.\d+)?)|font-size:\s*?(?<fontSize>\d+)|[^'""])*?\1[^>]*?>\s*?(<b>)?(?<text>((?!<(img|b)[^>]*?>)[\s\S])*?)\s*?(</b>)?</div>").OfType<Match>().Select(t => new { zIndex = t.Groups["zIndex"].Value, left = t.Groups["left"].Value, top = t.Groups["top"].Value, fontSize = t.Groups["fontSize"].Value, text = t.Groups["text"].Value }).ToList();
Regex re=Regex.Match(你的字符串,"<div style="Z-INDEX:[\w\W]+?(?<zindex>[\d]+);position:absolute;left:(?<left>[^;]+);top:(?<top>[^;]+);font-size:(?<fontsize>[\d]+);[\w\W]+?>(?<date>[^<]+)</div>");string a= re.groups[zindex].value;
string b= re.groups[left].value;
string c= re.groups[top].value;
string d= re.groups[fontsize].value;
string e= re.groups[date].value;
正则测试过的,没问题,但代码手动打的,可能大小写有点不对。你试下吧
/*
+ [0] { zIndex = "101", left = "23.17", top = "23.1", fontSize = "20", text = "日期(Date):2012年12月1日" } <Anonymous Type> */
string str = @"<div style='Z-INDEX:101;position:absolute;left:23.17mm;top:23.1mm;font-size:20;font-family:黑体;BACKGROUND-COLOR: white; color: black'>日期(Date):2012年12月1日</div>"; Match m = r.Match(str);
Console.WriteLine(m.Groups["center"].Value); r = new Regex("(?<name>[^'|\"|:]+):(?<value>[^'|\"|;]+);");
str = m.Groups["style"].Value; MatchCollection mc = r.Matches(str);
foreach (Match match in mc)
{
Console.WriteLine("name:{0}\tvalue:{1}", match.Groups["name"].Value,
match.Groups["value"]);
} Console.ReadKey();
这个可以,但是有点小问题
如下div的值就取不出来
<div style="Z-INDEX: 101;position:absolute;left:52.26mm;top:91.54mm;font-size:30;font-family:隶书;BACKGROUND-COLOR: white; color: black"><b>京剧《红灯记》</b></div>如下值没有取出来
<b>京剧《红灯记》</b>
历害啊。。
还有一点点小问题,就是取的值在有<b></b>时能不能把这个<b></b>去掉只取之间的那值,不然取出来还要要用replace
还有如下的情况不要取出来
<div style="Z-INDEX: 1;position:absolute;left:36.3mm;top:63.24mm;">
<img src="/design22/clients/barCode.aspx?Value=170131459582" style="width:47.63mm; height:10.68mm">
</div>
当内容是<img的时候就不要取这个div的值
都ok了,只有最后一点点小毛刺
<div style="Z-INDEX: 101;position:absolute;left:175.58mm;top:61.22mm;font-size:;font-family:黑体;BACKGROUND-COLOR: white; color: black">
<b>NO: 00002 </b></div>在这种html情况下,取的text的值是<b>NO: 00002