我的.dat文件包含很多行,如下所示:
2975,"Koltsovo Airport","Yekaterinburg","Russia","SVX","USSS",56.743099212646,60.802700042725,764,5,"N","Asia/Yekaterinburg","airport","OurAirports"
我需要的所有内容是按标识符搜索行,在本例中为USSS,并获得2个值:56.743099212646,60.802700042725。
我写了一些小代码,但我在C#中的水平不足以完成我的密码:(
string re1 = ".*?";
string re2 = "(\"USSS\")";
Regex g = new Regex(re1 + re2, RegexOptions.IgnoreCase | RegexOptions.Singleline);
using (StreamReader r = new StreamReader("airports.dat"))
{
string line;
while ((line = r.ReadLine()) != null)
{
Match m = g.Match(line);
if (m.Success)
{
string v = m.Groups[1].Value;
MessageBox.Show(v);
}
}
}
请帮助我,需要按标识符搜索哪些代码(例如USSS),并获得2个值56.743099212646,60.802700042725。
答案 0 :(得分:0)
看起来您不应该找到USSS,只需按\d+\.\d{12}
更新
带有ussUSSS\",(\d+\.\d{12}),(\d+\.\d{12})
组[1]和[2]的版本包含您的数据
答案 1 :(得分:0)
在USSS与#34;,#34;分隔后找两个号码。
将re2替换为:" USSS \",([0-9] + \。[0-9] +),([0-9] + \。[0-9] + )"
string re1 = ".*?";
string re2 = "USSS\",([0-9]+\\.[0-9]+),([0-9]+\\.[0-9]+)";
Regex g = new Regex(re1 + re2, RegexOptions.IgnoreCase | RegexOptions.Singleline);
using (StreamReader r = new StreamReader("airports.dat"))
{
string line;
while ((line = r.ReadLine()) != null)
{
Match m = g.Match(line);
if (m.Success)
{
string v1 = m.Groups[1].Value;
string v2 = m.Groups[2].Value;
MessageBox.Show(v1);
MessageBox.Show(v2);
}
}
}
答案 2 :(得分:0)
重播的Thnaks!使用正则表达式代码解决了问题:
\"USSS\",(\d+\.\d{12}),(\d+\.\d{12})
从组[1]和[2]
中获取我的值Regex g = new Regex(@"\""USSS\"",(\d+\.\d{12}),(\d+\.\d{12})", RegexOptions.IgnoreCase | RegexOptions.Singleline);
using (StreamReader r = new StreamReader("airports.dat"))
{
string line;
while ((line = r.ReadLine()) != null)
{
Match m = g.Match(line);
if (m.Success)
{
string v = m.Groups[1].Value;
string v2 = m.Groups[2].Value;
MessageBox.Show(v);
MessageBox.Show(v2);
}
// Do stuff with line.
}
}