所以我正在 C# 中做一个小项目,想要读取一个长文本文件,当它遇到该行时"X-Originating-IP: [192.168.1.1]"
我想获取 IP 并仅显示到控制台识别的 IP #,所以只需192.168.1.1
等等。我无法理解正则表达式。任何能让我开始的人都非常感激。到目前为止我所拥有的如下。
namespace x.Originating.Ip
{
class Program
{
static void Main(string[] args)
{
int counter = 0;
string line;
System.IO.StreamReader file =
new System.IO.StreamReader("C:\\example.txt");
while ((line = file.ReadLine()) != null)
{
if (line.Contains("X-Originating-IP: "))
Console.WriteLine(line);
counter++;
}
file.Close();
Console.ReadLine();
}
}
}
试试这个例子:
//Add this namespace
using System.Text.RegularExpressions;
String input = @"X-Originating-IP: [192.168.1.1]";
Regex IPAd = new Regex(@"\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b");
MatchCollection MatchResult = IPAd.Matches(input);
Console.WriteLine(MatchResult[0]);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)