日期:2014-05-18 浏览次数:20962 次
string content = ""; string regex = "slot=(?<slot>\\d+)\\s+port=(?<port>\\d+)\\s+vlan=(?<vlan>\\d+)\\s+PPP\\s+(?<src>.*?)\\s+\\" + "d+\\.\\d+\\.\\d+\\.\\d\\s+(?<mac>.*?)\\s+(?<date>\\d{4}-\\d{1,2}-\\d{1,2}\\s*\\d{1,2}:\\d{1,2}" + ":\\d{1,2})\\s+\\(55\\)(?<last>[\\S]+?\\s+[\\S]+?\\s+[\\S]+?)\\s"; System.Text.RegularExpressions.RegexOptions options = System.Text.RegularExpressions.RegexOptions.IgnoreCase; System.Text.RegularExpressions.Regex reg = new System.Text.RegularExpressions.Regex(regex, options); foreach (System.Text.RegularExpressions.Match item in reg.Matches(content)) { string solt = item.Groups["slot"].Value; . . . //以此类推,分别取其组的值,存入数据库 }
------解决方案--------------------
private void button1_Click(object sender, EventArgs e) { listBox1.Items.Clear(); Regex anExpr = new Regex(@"slot=(?<slot>\d+).*?port=(?<port>\d+).*?vlan=(?<vlan>\d+).*?(?<host>\w+@\w+).*?(?<ID>[\w|-]{17}).*?(?<date>[\d|:|-]{10}).*?(?<time>[\d|:|-]{8})\s+(?<tag>\(\d+\).*?)\s+参考.*?-+", RegexOptions.IgnoreCase | RegexOptions.Singleline); Match amt = anExpr.Match(textBox1.Text); while (amt.Success) { //匹配开始 listBox1.Items.Add("!!!!"); listBox1.Items.Add("slot=" + amt.Groups["slog"].Value); listBox1.Items.Add("port=" + amt.Groups["port"].Value); listBox1.Items.Add("host=" + amt.Groups["host"].Value); listBox1.Items.Add("date=" + amt.Groups["date"].Value); listBox1.Items.Add("id=" + amt.Groups["ID"].Value); listBox1.Items.Add("tag=" + amt.Groups["tag"].Value); listBox1.Items.Add("vlan=" + amt.Groups["vlan"].Value); listBox1.Items.Add(&quo