开发者

C# Regex replace help

I have a string:

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开发者_StackOverflow社区 want to write out

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.


I wouldn't use a Regex, but IndexOf() to find first "|", then replace on .Substring() from that position plus 1 till the end... a priori, it should perform better - but as always happens with performance, reality surprises ☺


+1 John. 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.


Since you asked for a regex:

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.

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜