日期:2014-05-19  浏览次数:20851 次

如何用正则怎么提取这段HTML中的内容,把取得的结果用DataGridView显示出来!
g_hotelresouce.change24269=[];
g_hotelresouce.change24269[0]   =   new   Array();
g_hotelresouce.change24269[0][0]   =   "2007/04/11 ";
g_hotelresouce.change24269[0][1]   =1688;
g_hotelresouce.change24269[0][2]   =   "现付 ";
g_hotelresouce.change24269[0][3]   =   "单早 ";
g_hotelresouce.change24269[0][4]   = "RMB ";
g_hotelresouce.change24269[1]   =   new   Array();
g_hotelresouce.change24269[1][0]   =   "2007/04/12 ";
g_hotelresouce.change24269[1][1]   =1688;
g_hotelresouce.change24269[1][2]   =   "现付 ";
g_hotelresouce.change24269[1][3]   =   "单早 ";
g_hotelresouce.change24269[1][4]   = "RMB ";
g_hotelresouce.change24269[2]   =   new   Array();
g_hotelresouce.change24269[2][0]   =   "2007/04/13 ";
g_hotelresouce.change24269[2][1]   =1688;
g_hotelresouce.change24269[2][2]   =   "现付 ";
g_hotelresouce.change24269[2][3]   =   "单早 ";
g_hotelresouce.change24269[2][4]   = "RMB ";
g_hotelresouce.change24269[3]   =   new   Array();
g_hotelresouce.change24269[3][0]   =   "2007/04/14 ";
g_hotelresouce.change24269[3][1]   =1688;
g_hotelresouce.change24269[3][2]   =   "现付 ";
g_hotelresouce.change24269[3][3]   =   "单早 ";
g_hotelresouce.change24269[3][4]   = "RMB ";

显示的DATAGRIDVIEW格式为
日期    价格               付款方式   早餐    币种
2007-4-11         -                         -                       -                       -
2007-4-12


没用过正则,希望大家能帮忙,非常感谢!



------解决方案--------------------
没有指定类型,细节上楼主自己处理下吧

string yourStr = richTextBox1.Text; //html源字符串
DataTable dt = new DataTable();
dt.Columns.Add( "日期 ");
dt.Columns.Add( "价格 ");
dt.Columns.Add( "付款方式 ");
dt.Columns.Add( "早餐 ");
dt.Columns.Add( "币种 ");
MatchCollection mc = Regex.Matches(yourStr, @ "new\s+Array()[\s\S]*?=\s* " "(? <time> [^ " "]*) " "[\s\S]*?=\s*(? <price> \S*)\s*;[\s\S]*?=\s* " "(? <type> [^ " "]*) " "[\s\S]*?=\s* " "(? <breakfast> [^ " "]*) " "[\s\S]*?=\s* " "(? <currency> [^ " "]*) " " ", RegexOptions.IgnoreCase);
foreach (Match m in mc)
{
DataRow dr = dt.NewRow();
dr[0] = m.Groups[ "time "].Value;
dr[1] = m.Groups[ "price "].Value;
dr[2] = m.Groups[ "type "].Value;
dr[3] = m.Groups[ "breakfast "].Value;
dr[4] = m.Groups[ "currency "].Value;
dt.Rows.Add(dr);
}

dataGridView1.DataSource = dt;