C# 更改字符串[]文件中的字符串=File.ReadAllLines(csspath);

C# 更改字符串[]文件中的字符串=File.ReadAllLines(csspath);,c#,regex,backend,C#,Regex,Backend,我尝试更改从函数创建的数组中的字符串 File.ReadAllLines(csspath) 我尝试使用Regex.Replace并将输出分配给我使用的字符串(在本例中为file)。 在函数中,我得到了数组和要将字符串更改为的字符串 问题是当我使用file.Replace(file,outp)时我需要保存新数组,我想我不知道如何处理此问题。 我应该如何处理这个解决方案 我还想使用Regex.Match函数,但我得到的是匹配,而不是替换 private static void MyFunction(


我尝试更改从函数创建的数组中的字符串
File.ReadAllLines(csspath)

我尝试使用
Regex.Replace
并将输出分配给我使用的
字符串(
在本例中为file
)。

在函数中,我得到了数组和要将字符串更改为的字符串

问题是当我使用
file.Replace(file,outp)时我需要保存新数组,我想我不知道如何处理此问题。

我应该如何处理这个解决方案

我还想使用
Regex.Match
函数,但我得到的是匹配,而不是替换

private static void MyFunction(string[] files,string changeto)
{
    var outp = "";

    foreach (var file in files)
    {
        if (file.Contains("font-family"))
        {
            //var match = Regex.Match(file, "'.*';");

            outp = Regex.Replace(file, "'.*';", changeto);

            file.Replace(file, outp);
        }
    }
}
我建议更改设计:让方法返回更改(而不是
void
):

或者您甚至可以完全放弃该方法,并获得可读的代码:


“它不工作”不是一个足够好的问题描述。你需要更具体地描述你期望发生的事情和实际发生的事情。请编辑您的问题并添加这些详细信息。首先确保字符串与正则表达式匹配。是否确定文件名中有
?因为我在
Regex
中看到了它们。您需要包含一个清晰的问题陈述,它不清楚什么是错误的,或者您希望它做什么。看起来好像您想更改文件扩展名,但是一般命名的方法,以及缺少描述,使得它在很大程度上不适合您的工作!。
private static IEnumerable<string> MyFuction(IEnumerable<string> lines, string changeTo) {
  return lines
    .Select(line => Regex.Replace(line, "'.*';", changeTo)); 
}
// 1. Read lines from the file
// 2. Make required changes
// 3. Organize the final result as an array
string[] data = MyFuction(File.ReadLines(csspath))
  .ToArray();
string[] data = File
  .ReadLines(csspath)                                     // read file
  .Select(line => Regex.Replace(line, "'.*';", changeTo)) // make changes
  .ToArray();                                             // materialize as an array