Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#正则表达式替换帮助_C#_Regex - Fatal编程技术网

C#正则表达式替换帮助

C#正则表达式替换帮助,c#,regex,C#,Regex,我有一个字符串: 应用程序1231 C:\asfae\drqw\qwer | 2342 | 1.txt 我有以下代码: Regex line2parse = Regex.Match(line,@"(\|)(\|)(\|)(\d)"); if (line2parse < 2) { File.AppendAllText(workingdirform2 + "configuration.txt", Regex-line2parse=Regex.Match(行,@“(\\\\)

我有一个字符串:

应用程序1231 C:\asfae\drqw\qwer | 2342 | 1.txt

我有以下代码:

 Regex line2parse = Regex.Match(line,@"(\|)(\|)(\|)(\d)");
 if (line2parse < 2)
 {

     File.AppendAllText(workingdirform2 + "configuration.txt",
Regex-line2parse=Regex.Match(行,@“(\\\\)(\\\\\)(\\\\\)(\\\\\)(\d)”;
如果(line2parse<2)
{
文件.AppendAllText(workingdirform2+“configuration.txt”,
我想做的是将第一个
之后的每个
替换为
\
所以我想写下来

应用程序1231 | C:\asfae\drqw\qwer\2342\1.txt


我不会使用正则表达式,但IndexOf()首先查找“|”,然后替换.Substring(),从该位置加1直到结束……从先验角度来看,它应该表现得更好——但与性能一样,现实令人惊讶☺

您可以在不使用正则表达式的情况下执行此操作:

string line = @"Apple1231|C:\asfae\drqw\qwer|2342|1.txt";
string[] parts = line.Split('|');
string clean = parts[0] + "|" + string.Join(@"\", parts, 1, parts.Length - 1);

string.Join调用使用一个重载,允许您指定一个开始索引来跳过第一项。

因为您要求一个正则表达式:

var result = Regex.Replace( line, @"(.+?)\|(.+?)\|(.+?)\|(.+?)", "$1|$2\\$3\\$4");

完全未经测试,但请尝试

fixed = Regex.Replace(unfixed.replace("|", @"\"), @"\\", "|", 1);
也就是说-将所有的斜杠变成斜杠,然后将第一个斜杠变成一个斜杠。

不要使用正则表达式

        string input = @"Apple1231|C:\asfae\drqw\qwer|2342|1.txt";
        int firstPipeIndex = input.IndexOf("|");
        string suffix = string.Empty;
        string prefix = string.Empty;
        string output = string.Empty;
        if (firstPipeIndex != -1)
        {
            //keep the first pipe and anything before in prefix
            prefix = input.Substring(0, firstPipeIndex + 1);
            //all pipes in the rest of it should be slashes
            suffix = input.Substring(firstPipeIndex + 1).Replace('|', '\\');
            output = prefix + suffix;
        }
        if (!string.IsNullOrEmpty(suffix))
        {
            Console.WriteLine(input);
            Console.WriteLine(output);
        }

下面是应该实现这一技巧的代码:

Regex MyRegex = new Regex( @"(.+\|)(.+)\|(\d{1,})\|(.+)", RegexOptions.IgnoreCase | RegexOptions.CultureInvariant | RegexOptions.IgnorePatternWhitespace | RegexOptions.Compiled ); // This is the replacement string string MyRegexReplace = @"$1$2\$3\$4"; //// Replace the matched text in the InputText using the replacement pattern string result = MyRegex.Replace(@"Apple1231|C:\asfae\drqw\qwer|2342|1.txt",MyRegexReplace); // result 'Apple1231|C:\asfae\drqw\qwer\2342\1.txt' 正则表达式MyRegex=新正则表达式( @“(.+\\\\)(.+)\\\\\\\(\d{1,})\\\\\\(.+)”, RegexOptions.IgnoreCase |RegexOptions.CultureInvariant |RegexOptions.IgnorePatternWhitespace |RegexOptions.Compiled ); //这是替换字符串 字符串MyRegexReplace= @"$1$2\$3\$4"; ////使用替换模式替换InputText中匹配的文本 字符串结果=MyRegex.Replace(@“Apple1231|C:\asfae\drqw\qwer|2342|1.txt”,MyRegexReplace); // 结果“Apple1231 | C:\asfae\drqw\qwer\2342\1.txt” 希望这有帮助, 顺致敬意, 汤姆。

+1约翰。 显然正则表达式不是这里的最佳解决方案,但我的观点是:

string original = @"Apple1231|C:\asfae\drqw\qwer|2342|1.txt";
Regex pattern = new Regex(@"(?<=.*\|)(?'rep'[^\|]*)\|");
string result = pattern.Replace(original, @"${rep}\");
string original=@“Apple1231|C:\asfae\drqw\qwer|2342|1.txt”;

Regex pattern=new Regex(@)(?此解决方案仅在堆上创建两个新对象,而不是其他解决方案所需的N个对象

static string FixString(string line)
{
    if (line == null)
        return string.Empty;

    int firstBarPosition = line.IndexOf('|');
    if (firstBarPosition == -1 || firstBarPosition + 1 == line.Length)
        return line;

    StringBuilder sb = new StringBuilder(line);

    sb.Replace('|', '\\', firstBarPosition + 1, line.Length - (firstBarPosition + 1));
    return sb.ToString();
}

你的代码很不完整。你能提供你的
if
语句的其余部分吗,这样你问题中的例子就更完整了?我还没有完成它,因为我不知道正则表达式,这就是要写出来的所有内容。原始的问题语句并不表示在之前的字符串中不能出现斜杠你是第一条,所以这个解决方案是不正确的。哇,这比我的解决方案好多了。