我是编程新手,一直在努力解析文件。我,最初试图以某种方式解析它,但最终没有正确地工作。我想解析Dictionary<字符串string>中的以下行。
网卡:安装了7个网卡。
[01]: Broadcom
Connection Name: Local Area Connection
DHCP Enabled: No
IP address(es)
[01]: abc.de.xyz.
[02]: Broadcom
Connection Name: eth1
Status: Media disconnected
[03]: Broadcom
Connection Name: eth0
Status: Media disconnected
[04]: Broadcom
Connection Name: eth3
Status: Media disconnected
[05]: Mellanox
Connection Name: Local Area Connection 5
Status: Hardware not present
[06]: Mellanox
Connection Name: Local Area Connection 6
Status: Media disconnected
[07]: Mellanox
Connection Name: Local Area Connection 7
DHCP Enabled: No
IP address(es)
[01]: mno.pqr.stu.vwx我希望将01 Broadcom作为字典的键,连接名称为: Local Area Connection DHCP Enabled: No IP address(es) 01: abc.de.xyz作为其他六个键的值,依此类推。谢谢你的帮助。真的很感谢。任何关于如何做到这一点的帮助都将是很好的,因为我已经疯狂地阅读了关于拆分字符串的内容,并试图弄清楚如何让字典存储值。
发布于 2011-05-19 12:29:52
如果你不想走这条路,这里有一个不使用正则表达式的解决方案。这段代码已经过测试。
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
namespace NicParser
{
public class NicFileParser
{
private readonly string _file;
private readonly Dictionary<string, string> _nics;
public NicFileParser(string file)
{
_file = file;
_nics = new Dictionary<string, string>();
}
public void Parse()
{
var key = string.Empty;
var value = new StringBuilder();
try
{
using (var rdr = new StreamReader(_file))
{
var firstTime = true;
while (rdr.Peek() > 0)
{
var line = rdr.ReadLine().Trim();
if (IsKey(line))
{
// Once a key is hit, add the previous
// key and values (except the first time).
if (!firstTime)
{
_nics.Add(key, value.ToString());
}
else
{
firstTime = false;
}
// Assign the key, and clear the previous values.
key = line;
value.Length = 0;
}
else
{
// Add to the values for this nic card.
value.AppendLine(line);
}
}
// Final line of the file has been read.
// Add the last nic card.
_nics.Add(key, value.ToString());
}
}
catch (Exception ex)
{
// Handle your exceptions however you like...
}
}
private static bool IsKey(string line)
{
return (!String.IsNullOrEmpty(line)
&& line.StartsWith("[")
&& !line.Contains("."));
}
// Use this to access the NIC information.
public Dictionary<string, string> Cards
{
get { return _nics; }
}
}
}发布于 2011-05-19 12:17:24
请原谅任何糟糕的C#语法--我已经习惯了VB .NET,请不要笑。
我会先将文件的文本行读入字符串数组。
foreach (string line in File.ReadLines("path-to-file")) {
}对于每一行,您要么在"key“行上,要么在"value”行上。关键行如下所示:
[01]: Broadcom要确定您是否在"key“线路上,您可以尝试类似line.Trim().StartsWith("[")的命令,但这不会可靠地工作,因为您还有其他类似[01]: abc.def.ghi.jkl的线路,它们是IP地址,而不是key。因此,您需要更聪明一点,甚至可以使用正则表达式来检测您正在查看的是IP地址还是网卡。我不知道您正在查看的文件的确切规格,但您也可以使用前导空格/制表符来帮助您确定您是在"key“行还是在"value”行。
然后,您的代码将如下所示:
var networkCards = new Dictionary<String, String>();
string currentKey = String.Empty;
foreach (string line in File.ReadLines("path-to-file")) {
if ( IsKeyLine( line ) ) {
currentKey = line.Trim();
networkCards.Add(currentKey, "");
} else {
networkCards[currentKey] += line.Trim() + " ";
}
}需要编写IsKeyLine方法,它是整个操作的关键。以下是您可能使用的基于正则表达式的方法:
public bool IsKeyLine(string line) {
if (!String.IsNullOrEmpty(line)) {
//run two regexes - one to see if the line is of the general pattern of a "key" line
//the second reg ex makes sure there isn't an ip address in the line, which would indicate that the line is part of the "value" and not the "key"
return System.Text.RegularExpressions.RegEx.IsMatch(line, @"^\s*\[\d{0,2}\]: ")
&& !System.Text.RegularExpressions.RegEx.IsMatch(line, @"\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}");
}
return false;
}现在,我没有花时间测试这些代码-这是我的头顶。但它至少应该让你朝着正确的方向前进。不过,要确定的最大问题是文件格式的标准。这将给你一些线索,让你走上正确的道路。您甚至可能不需要正则表达式(这会更好,因为regex的运行成本通常很高)。
发布于 2011-05-19 12:24:26
您还可以计算每行开头的制表符/空格,以指示该行所属的位置。
https://stackoverflow.com/questions/6053447
复制相似问题