using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
namespace RegExCs
{
class Program
{
static void Main(string[] args)
{
string rawData;
Regex filter = new Regex(@"(?<ip>([0-9]+\.){3}[0-9])"+@"(?<time>(\s[0-2][0-9]:[0-9][0-9]))");
rawData=File.ReadAllText("Query list");
MatchCollection theMatches = filter.Matches(rawData);
foreach (Match theMatch in theMatches)
{
Console.WriteLine("ip: {0}\n",theMatch.Groups["ip"]);
Console.WriteLine("time: {0}\n", theMatch.Groups["time"]);
}
Console.ReadKey();
}
}
}
“查询列表”文件:
Reply from 212.77.100.101 www.wp.pl time: 21:37 Reply from 111.41.130.55 www.cnn.com time: 05:33 Reply from 230.77.100.101 www.piting.com time: 04:12 Reply from 65.77.100.101 www.ha.org time: 12:55 Reply from 200.77.100.101 www.example.com time: 07:56
此程序编译并运行,但空控制台窗口一直打开。为什么呢?
答案 0 :(得分:3)
因为没有任何东西与正则表达式匹配
@"(?<ip>([0-9]+\.){3}[0-9])(?<time>(\s[0-2][0-9]:[0-9][0-9]))"
你只是连接2个字符串,复合正则表达式希望字符串为ip
后跟time
,而它们之间没有任何其他东西(甚至空格)。
您需要将其更改为
@"(?<ip>([0-9]+\.){3}[0-9]).*(?<time>(\s[0-2][0-9]:[0-9][0-9]))"
^------- "anything" between first and second group