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

这个字符串的正则表达式这么写啊?
html里面有一个javascript的数组现在要把里面数组转换成C#里面的数组
var   gidx=new   Array();
var   GameFTRB=new   Array();
GameFTRB[1]=new   Array( '67390 ', '2007-04-04   06:00:00 ');
GameFTRB[2]=new   Array( '67390 ', '2007-04-04   06:00:00 ');
GameFTRB[3]=new   Array( '67390 ', '2007-04-04   06:00:00 ');
GameFTRB[4]=new   Array( '67390 ', '2007-04-04   06:00:00 ');
GameFTRB[5]=new   Array( '67390 ', '2007-04-04   06:00:00 ');
......
GameFTRB[n]=new   Array( '67390 ', '2007-04-04   06:00:00 ');

怎么用正则表达式去括号里面的数据?

------解决方案--------------------
string str = @ " var gidx=new Array(); var GameFTRB=new Array(); GameFTRB[1]=new Array( '67390 ', '2007-04-04 06:00:00 '); GameFTRB[2]=new Array( '67390 ', '2007-04-04 06:00:00 '); GameFTRB[3]=new Array( '67390 ', '2007-04-04 06:00:00 '); GameFTRB[4]=new Array( '67390 ', '2007-04-04 06:00:00 '); GameFTRB[5]=new Array( '67390 ', '2007-04-04 06:00:00 '); ...... GameFTRB[n]=new Array( '67390 ', '2007-04-04 06:00:00 '); "; Regex reg = new Regex(@ "Array\( '([^ ']*?) ', '([^ ']*?) '); ",RegexOptions.Multiline); MatchCollection ms = reg.Matches(str); for (int i = 0; i < ms.Count; i++) { Response.Write(ms[i].Result( "$1 ") + ", " + ms[i].Result( "$2 ") + " <BR> "); }
------解决方案--------------------
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;

namespace ConverArray
{
class Item
{
public int id;
public string datetime;
}

class Program
{
static string s = @ "
var gidx=new Array();
var GameFTRB=new Array();
GameFTRB[1]=new Array( '67390 ', '2007-04-04 06:00:00 ');
GameFTRB[2]=new Array( '67390 ', '2007-04-04 06:00:00 ');
GameFTRB[3]=new Array( '67390 ', '2007-04-04 06:00:00 ');
GameFTRB[4]=new Array( '67390 ', '2007-04-04 06:00:00 ');
GameFTRB[5]=new Array( '67390 ', '2007-04-04 06:00:00 ');
GameFTRB[6]=new Array( '67390 ', '2007-04-04 06:00:00 '); ";

static void Main(string[] args)
{
Regex regex = new Regex(@ "new Array\( '(? <ID> [^ ']*) ', '(? <DateTime> [^ ']*) '\) ");
MatchCollection mc = regex.Matches(s);
Item[] items = new Item[mc.Count];
int i = 0;
foreach (Match match in mc)
{
Item item = new Item();
item.id = int.Parse(match.Groups[ "ID "].Value);
item.datetime = match.Groups[ "DateTime "].Value;
items[i++] = item;
}

for (int j = 0; j < items.Length; ++j)
{
Console.WriteLine( "item[{0}]=[id: {1}][datetime: {2}] ", j, items[j].id, items[j].datetime);
}
}
}
}