0

I know what is going on, but i was trying to make it so that my .Split() ignores certain characters.

sample:

1|2|3|This is a string|type:1

the parts "This is a string" is user input The user could enter in a splitting character, | in this case, so i wanted to escape it with \|. It still seems to split based on that. This is being done on the web, so i was thinking that a smart move might actually be just JSON.encode(user_in) to get around it?

1|2|3| This is \|a string|type:1

Still splits on the escaped character because i didnt define it as a special case. How would i get around this issue?

Fallenreaper
  • 10,222
  • 12
  • 66
  • 129
  • Seems like `This is \|a string` should be surrounded by quotes, as in `"This is \|a string"`. That would make it work properly with the CSV parsers; the only user input that wouldn't work is an inner quotation mark. – Robert Harvey Aug 23 '12 at 18:03

2 Answers2

3

you could use Regex.Split instead and then split on | not preceded by a .

        // -- regex for | not preceded by a \
        string input = @"1|2|3|This is a string\|type:1";
        string pattern = @"(?<!\\)[|]";
        string[] substrings = Regex.Split(input, pattern);

        foreach (string match in substrings)
        {
            Console.WriteLine("'{0}'", match);
        }
John Sobolewski
  • 4,512
  • 1
  • 20
  • 26
  • So, instead of javascript doign something like x.Split("|") i would do something like x.Split(/(\\)|/); Its been a bit since ive done regex in javascript – Fallenreaper Aug 23 '12 at 18:12
  • Im going to see if i can give this a go and see if this will work. – Fallenreaper Aug 23 '12 at 18:15
  • 1
    If you need to do this in javascript this question/answer should point you in the correct direction: http://stackoverflow.com/questions/641407/javascript-negative-lookbehind-equivalent – John Sobolewski Aug 23 '12 at 18:16
0

You can replace your delimiter with something special first, next split it and finally replace it back.

    var initial = @"1|2|3|This is \| a string|type:1";
    var modified = initial.Replace(@"\|", "@@@");
    IEnumerable<string> result = modified.Split('|');
    result = result.Select(i => i.Replace("@@@", @"\|"));
user854301
  • 5,383
  • 3
  • 28
  • 37