日期:2014-05-16  浏览次数:20984 次

请教用正则表达式提取4个字符串
<TD width=58 row="1">1.905</TD>
<TD row="1" ref="-2.020">成本/售价</TD>
<TD width=58 row="1" class=””>3.925</TD>

从上面的字符串中提取1.905,-2.020,成本/售价,3.925这四个。

------解决方案--------------------
(?<=ref\=\").*?(?=\"\>)
(?<=\>).*?(?=\<\/TD\>)
------解决方案--------------------
 string sInput = File.ReadAllText(@"C:\Users\myx\Desktop\Test.txt", Encoding.GetEncoding("GB2312"));
            string pattern = @"(?i)<td[^>]*?>(\d+(?:\.\d+)?)</td>\s*?<td[^>]*?ref=(['""]?)([^'""]*?)\2[^>]*?>([^<>]*?)</td>\s*?<td[^>]*?>([^<>]*?)</td>";
            Match m = Regex.Match(sInput, pattern);

            string v1 = m.Groups[1].Value;//"1.905"
            string v2 = m.Groups[3].Value;//"2.020"
            string v3 = m.Groups[4].Value;//"成本/售价"
            string v4 = m.Groups[5].Value;//"3.925"