如何拆分/分组文本文件?

发布于 2024-11-24 14:40:32 字数 301 浏览 0 评论 0原文

我想拆分/分组此文本:

[Help]

Group0

[Help Op]

Group1

[Help Mod]

Group2

[Help Member]

Group3

[Help Default]

Group4

我该怎么做? 因此,像

string op 等于 Group1,或者标签 [Help OP] 下的任何文本

与其他“标签”相同,例如 string default 将等于“group4”,

或者即使有更好的方法来布局它,例如 xml 或无论如何。

I want to split/group this text:

[Help]

Group0

[Help Op]

Group1

[Help Mod]

Group2

[Help Member]

Group3

[Help Default]

Group4

How may I do so?
So Like

string op would equal Group1, or whatever text is underneath the tag [Help OP]

same with the other 'tags' like string default would equal 'group4'

or even if there is a better way to lay this out such as xml or whatever else.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

(り薆情海 2024-12-01 14:40:32

如果您使用 xml 文件,则可以利用 LINQ-TO-XML 来处理解析操作。如果您使用 ini 文件或自定义版本之类的文件,则必须编写自己的解析代码。虽然不是很困难,但仍然需要额外的工作。如果您不介意使用尖括号税,请查看 LINQ-TO-XML。 这里是一个可以帮助您入门的教程。

注意:我假设您正在使用 .Net 3.5 或更高版本。如果情况并非如此,那么使用 XML 的工作量就更大了。

更新:

如果您更倾向于 ini 方法,您的文件将如下所示:

[帮助]
操作 = 组 1
Mod=组2
成员=组3
默认=Group4

[其他一些部分]
someKey=someValue

等。

我编写了一个简单的库,可以解析 ini 文件,读取键和值,写入键和值并保存 ini 文件更改(通过覆盖或通过保存到新文件)。代码太大,无法在这里发布,但我总是可以将其扔到某个地方并放置一个链接。

要查找一个值,将类似于以下内容:

var ini = new IniFile(fileName);

string myValue;

// look up section Help, key Op
if (ini.TryGetValue("Help", "Op", out myValue))
{
    // do something with myValue, which would contain "Group1"
}

// if the result was false, it means the section or the key did not exist.

If you go with an xml file, you can take advantage of LINQ-TO-XML to handle the parsing operations. If you go with something like an ini file or a custom version like you have, you'll have to write your own parsing code. Not terribly tough, but extra work none the less. Take a look at LINQ-TO-XML if you don't mind working with the angle bracket tax. Here is a tutorial that may get you started.

NOTE: I'm assuming you're working with .Net 3.5 or higher. If that is not the case, working with XML is a bit more work.

UPDATE:

If you're leaning more towards an ini approach, your files will look like this:

[Help]
Op=Group1
Mod=Group2
Member=Group3
Default=Group4

[Some Other section]
someKey=someValue

etc.

I've got a simple library that I wrote that can parse through the ini file, reading keys and values, writing keys and values and saving ini file changes (either to the same location by overwriting or by saving to a new file). The code is too big to post in here, but I could always toss it up somewhere and put a link to it.

To look up a value it would be something like the following:

var ini = new IniFile(fileName);

string myValue;

// look up section Help, key Op
if (ini.TryGetValue("Help", "Op", out myValue))
{
    // do something with myValue, which would contain "Group1"
}

// if the result was false, it means the section or the key did not exist.
苯莒 2024-12-01 14:40:32

kernel32.dll 中定义了 2 个 Windows 外部函数来读取/写入 .ini 文件。
您需要将它们导入到您的代码中,然后使用它们:

在 VB6 中我曾经这样做:

Declare Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpDefault As String, ByVal lpReturnedString As String, ByVal nSize As Long, ByVal lpFileName As String) As Long

Declare Function WritePrivateProfileString Lib "kernel32" Alias "WritePrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpString As Any, ByVal lpFileName As String) As Long

在 C# 中您这样做:

[DllImport("kernel32")]
internal static extern int WritePrivateProfileString(string lpAppName, string lpKeyName, string lpString, string lpFileName);

[DllImport("kernel32")]
internal static extern int GetPrivateProfileString(string lpAppName, string lpKeyName, string lpDefault, string lpReturnedString, int nSize, string lpFileName);

There are 2 Windows external functions defined in kernel32.dll to read/write .ini files.
You need to import them into your code and then use them:

In VB6 I used to do this:

Declare Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpDefault As String, ByVal lpReturnedString As String, ByVal nSize As Long, ByVal lpFileName As String) As Long

Declare Function WritePrivateProfileString Lib "kernel32" Alias "WritePrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpString As Any, ByVal lpFileName As String) As Long

In C# you do it like this:

[DllImport("kernel32")]
internal static extern int WritePrivateProfileString(string lpAppName, string lpKeyName, string lpString, string lpFileName);

[DllImport("kernel32")]
internal static extern int GetPrivateProfileString(string lpAppName, string lpKeyName, string lpDefault, string lpReturnedString, int nSize, string lpFileName);
无声无音无过去 2024-12-01 14:40:32

您似乎想读取包含以提供的格式存在的密钥和数据的 ini 文件。所以,如果你想从文本文件读取数据,你可以这样做

const string HELP = "[Help]", HELP_OP = "[Help Op]", HELP_MOD = "[Help Mod]", HELP_MEMBER = "[Help Member]", HELP_DEFAULT = "[Help Default]";
          string [] strConfig = File.ReadAllLines("G:\\Test.txt");
          Dictionary<string, List<string>> myConfigConfig = new Dictionary<string, List<string>>();
          string curr = string.Empty;
          foreach (string str in strConfig)
          {
              if (str == string.Empty)
                  continue;
              if (str == HELP || str == HELP_OP || str == HELP_MOD || str == HELP_MEMBER || str == HELP_DEFAULT)
              {
                  myConfigConfig.Add(str, new List<string>());
                  curr = str;  
              }
              else
                  myConfigConfig[curr].Add(str);
          }

It seems that you want to read the ini file with key and data existing in the provided format. So, if you want to read the data from text file, you can do

const string HELP = "[Help]", HELP_OP = "[Help Op]", HELP_MOD = "[Help Mod]", HELP_MEMBER = "[Help Member]", HELP_DEFAULT = "[Help Default]";
          string [] strConfig = File.ReadAllLines("G:\\Test.txt");
          Dictionary<string, List<string>> myConfigConfig = new Dictionary<string, List<string>>();
          string curr = string.Empty;
          foreach (string str in strConfig)
          {
              if (str == string.Empty)
                  continue;
              if (str == HELP || str == HELP_OP || str == HELP_MOD || str == HELP_MEMBER || str == HELP_DEFAULT)
              {
                  myConfigConfig.Add(str, new List<string>());
                  curr = str;  
              }
              else
                  myConfigConfig[curr].Add(str);
          }
绝情姑娘 2024-12-01 14:40:32
            string s = @"
[Help]

Group0

[Help Op]

Group1

[Help Mod]

Group2

[Help Member]

Group3

[Help Default]

Group4";

            MatchCollection matches = new Regex(@"\[Help ([a-zA-Z]+)\]\s+([^[]+)\s+", RegexOptions.Singleline).Matches(s);

            Dictionary<string, string> result = new Dictionary<string, string>();
            foreach (Match match in matches)
                result[match.Groups[1].Value] = match.Groups[2].Value;

            Console.WriteLine(result["Op"]);
            string s = @"
[Help]

Group0

[Help Op]

Group1

[Help Mod]

Group2

[Help Member]

Group3

[Help Default]

Group4";

            MatchCollection matches = new Regex(@"\[Help ([a-zA-Z]+)\]\s+([^[]+)\s+", RegexOptions.Singleline).Matches(s);

            Dictionary<string, string> result = new Dictionary<string, string>();
            foreach (Match match in matches)
                result[match.Groups[1].Value] = match.Groups[2].Value;

            Console.WriteLine(result["Op"]);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文