0

好的,我有一个文本文件,其中包含:

    books_book1 = 1
    books_book2 = 2
    books_book3 = 3

我想保留“books_book1 =”

到目前为止,我有:

string text = File.ReadAllText("settings.txt");
text = Regex.Replace(text, ".*books_book1*.", "books_book1 = a",
RegexOptions.Multiline);
File.WriteAllText("settings.txt", text);
text = Regex.Replace(text, ".*books_book2*.", "books_book2 = b",
RegexOptions.Multiline);
File.WriteAllText("settings.txt", text);
text = Regex.Replace(text, ".*books_book3*.", "books_book3 = c",
RegexOptions.Multiline);
File.WriteAllText("settings.txt", text);

这导致:

book_book1 = a=1

输出到文件应该是:

    books_book1 = a
    books_book2 = b
    books_book3 = c

非常感谢提前...

4

4 回答 4

2

我在评论中说:

“如果它这么简单,我个人会去重新创建文件。大概你最初将文件中的所有值加载到某种对象中,所以只需使用它来重新创建具有新值的文件。比搞砸容易得多正则表达式——它更简单,更容易测试,看看发生了什么,如果你需要的话,更容易改变。”

我认为再看一遍它就更真实了。

根据您在评论中所说:“当程序加载时,它会从此文本文件中读取值,然后用户可以选择更改值并将其保存回文件”。大概这意味着您实际上需要知道要替换的书 1、书 2 等行中的哪一行,这样您就知道要输入哪些用户提供的值。这对三个项目来说很好(虽然有点笨拙)但是如果你增加该数字,那么您需要为每个新项目更新您的代码。这绝不是一件好事,并且会很快产生一些看起来非常可怕的代码,很容易出现错误。

如果您在某种数据结构(例如字典)中有新设置,那么正如我所说,从头开始重新创建文件可能是最简单的。例如,请参阅这个完整的小代码片段:

//Set up our sample Dictionary
Dictionary<string, string> settings = new Dictionary<string,string>();
settings.Add("books_book1","a");
settings.Add("books_book2","b");
settings.Add("books_book3","c");

//Write the values to file via an intermediate stringbuilder.    
StringBuilder sb = new StringBuilder();
foreach (var item in settings)
{
    sb.AppendLine(String.Format("{0} = {1}", item.Key, item.Value));
}

File.WriteAllText("settings.txt", sb.ToString());

这具有更简单的明显优势,并且如果您添加更多设置,那么它们只会进入字典,您无需更改代码。

于 2013-06-19T19:46:55.683 回答
2

我认为这不是解决问题的最佳方法,但要让 RegEx 做你想做的事,你可以执行以下操作:

var findFilter = @"(.*books_book1\s*=\s)(.+)";
var replaceFilter = "${1}a"    
text = Regex.Replace(text, findFilter, replaceFilter, RegexOptions.Multiline)   
File.WriteLine("settings.txt", text);
....

(在这种情况下,正则表达式中的和之间的代码)是第一个也是唯一的反向引用捕获组,并且${1}在替换部分将使用匹配的组文本来创建您想要的输出。此外,您会注意到我使用\s了空白空间,因此您与 book111 不匹配。我确定您还需要处理其他边缘情况。

books_book1 = a
...
于 2013-06-19T19:07:29.763 回答
1

这是更通用方法的开始:

此正则表达式捕获最后一个数字,注意考虑数字和空白长度的可变性。

text = Regex.Replace(text , @"(books_book\d+\s*=\s*)(\d+)", DoReplace)

// ... 

string DoReplace(Match m)
{
    return m.Groups[1].Value + Convert.ToChar(int.Parse(m.Groups[2].Value) + 96);
}
于 2013-06-19T21:26:57.737 回答
0

像这样的东西怎么样(没有错误检查):

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace TestRegex
{
    class Program
    {
        static void Main( string[] args )
        {
            var path = @"settings.txt";
            var pattern = @"(^\s*books_book\d+\s*=\s*)(\d+)(\s*)$";
            var options = RegexOptions.IgnoreCase | RegexOptions.Multiline;
            var contents = Regex.Replace( File.ReadAllText( path ), pattern, MyMatchEvaluator, options );
            File.WriteAllText( path, contents );
        }

        static int x = char.ConvertToUtf32( "a", 0 );

        static string MyMatchEvaluator( Match m )
        {
            var x1 = m.Groups[ 1 ].Value;
            var x2 = char.ConvertFromUtf32( x++ );
            var x3 = m.Groups[ 3 ].Value;
            var result = x1 + x2 + x3;
            return result;
        }
    }
}
于 2013-06-19T19:59:13.627 回答