繁体   English   中英

Regex.Replace包含大字符串和反斜杠

[英]Regex.Replace with large strings and backslashes

我编写了一个实用程序,它打开一个基于文本的文件,load作为一个字符串,并使用RegEx.Replace执行查找/替换功能。

它在许多文件上执行此操作,用户将其指向文件夹,输入查找字符串,替换字符串以及文件夹中包含文件中的字符串的所有文件都被替换。

这很有效,直到我用反斜杠尝试它才会失败。

很简单:

newFileContent = Regex.Replace(fileContent, @findString, @replaceString, RegexOptions.IgnoreCase);

fileContent =基于文本的文件的内容。 它将包含回车。

findString =用户输入要查找的字符串

replaceString =用户输入的字符串替换找到的字符串

我已经尝试添加一些逻辑来反击下面的反斜杠,但是在模式结束时这会失败。

 if (culture.CompareInfo.IndexOf(findString, @"\") >= 0)
     {
      Regex.Replace(findString, @"\", @"\\");
     }

我需要做什么才能成功处理反斜杠,以便它们可以成为查找/替换逻辑的一部分?

下面是整个代码块。

//open reader
                using (var reader = new StreamReader(f,Encoding.Default)) 
                {
                    //read file
                    var fileContent = reader.ReadToEnd();

                    Globals.AppendTextToLine(string.Format(" replacing string"));

                    //culture find replace
                    var culture = new CultureInfo("en-gb", false);
                    //ensure nothing has changed
                    if (culture.CompareInfo.IndexOf(fileContent, findString, CompareOptions.IgnoreCase) >= 0)
                    {

                        //if find or replace string contains backslahes
                        if (culture.CompareInfo.IndexOf(findString, @"\") >= 0)
                        {
                            Regex.Replace(findString, @"\", @"\\");
                        }

                        //perform replace in new string
                        if (MainWindow.Main.chkIgnoreCase.IsChecked != null && (bool) MainWindow.Main.chkIgnoreCase.IsChecked)                        
                            newFileContent = Regex.Replace(fileContent, @findString, @replaceString, RegexOptions.IgnoreCase);
                        else
                            newFileContent = Regex.Replace(fileContent, @findString, @replaceString);

                        result[i].Result = true;
                        Globals.AppendTextToLine(string.Format(" success!"));
                    }
                    else
                    {
                        Globals.AppendTextToLine(string.Format(" failure!!"));
                        break;
                    }
                }

将用户输入传递给Replace方法时,应该使用Regex.Escape

通过用它们的转义码替换它们来转义一组最小字符(\\,*,+,?,|,{,[,(,),^,$ ,.,#和空格)。 这指示正则表达式引擎按字面解释这些字符而不是元字符。

例如:

newFileContent = Regex.Replace(fileContent,
                               Regex.Escape(findString),
                               replaceString,
                               RegexOptions.IgnoreCase);

你的根本问题是你让你的用户输入一个任意的正则表达式,因此,它被解释为正则表达式...

要么你的目标只是替换文字字符串,在这种情况下使用String.Replace或者你允许用户输入正则表达式,在这种情况下只需接受用户将需要\\转义他们的特殊字符。

因为\\是一个regexp转义字符(以及c#one,但你似乎用@处理它)“\\”是一个非法的正则表达式,因为你逃避了什么

如果你真的想要一个rexexp用\\\\替换所有\\,那么它:

Regex.Replace(findString, @"\\", @"\\\\"); --ie one \ after escape, two chars after escape.

但你仍然需要担心[]。?*等。

我的强烈建议是一个复选框,用户可以选择是否输入正则表达式或字符串文字进行替换,然后相应地调用String.Replace或Regex.Replace。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM