简体   繁体   English

C#Regex替换帮助

[英]C# Regex replace help

I have a string: 我有一个字符串:

Apple1231|C:\\asfae\\drqw\\qwer|2342|1.txt Apple1231 | C:\\ asfae \\ drqw \\ QWER | 2342 |的1.txt

I have the following code: 我有以下代码:

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

     File.AppendAllText(workingdirform2 + "configuration.txt",

What I want to be able to do is replace every | 我想要做的就是更换每个| after the first | 在第一个| with \\ So i want to write out \\所以我想写出来

Apple1231|C:\\asfae\\drqw\\qwer\\2342\\1.txt Apple1231 | C:\\ asfae \\ drqw \\ QWER \\ 2342 \\ 1.TXT

You could do this without regex: 没有正则表达式你可以做到这一点:

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);

The string.Join call uses an overload that lets you specify a start index to skip the first item. string.Join调用使用重载,允许您指定开始索引以跳过第一个项目。

我不会使用正则表达式,但IndexOf()首先找到“|”,然后从该位置加上.Substring()加1直到结束...先验,它应该表现更好 - 但总是发生在表现,现实惊喜☺

+1 John. +1约翰。 Obviously regular expressions are not the best solution here, but here's my take: 显然正则表达式不是最好的解决方案,但这是我的看法:

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

This is more generic than strictly necessary because it will handle an arbitrary number of replacements. 这比非常必要的更通用,因为它将处理任意数量的替换。

既然你要求正则表达式:

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

Entirely untested, but try 完全未经测试,但试试

fixed = Regex.Replace(unfixed.replace("|", @"\"), @"\\", "|", 1);

that is- turn all the bars into slashed, then turn the first slash back into a bar. 那就是把所有的条都变成斜线,然后把第一个斜线转回一个条。

Don't use regex. 不要使用正则表达式。

        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);
        }

Here's the code that should do the trick: 这是应该做的诀窍的代码:

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'

Hope this helps, Best regards, Tom. 希望这会有所帮助,最好的问候,汤姆。

This solution only creates two new objects on the heap rather than N objects that the other solutions require. 此解决方案仅在堆上创建两个新对象,而不是其他解决方案所需的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();
}

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

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