locked
Split string using Regex with escape sequences RRS feed

  • Question

  • What is the regex to split a string using the delimiter ';', but not split on '\;' ? Backslash would be my escape character in my split string, so finding '\;' would not split, but '\\;' would split.

    For example I want a regex that would split the following:

        "aaa;bb;cccc\;dd\;eee\\;ff\\;ggg\\\;hhh"

    into the following string array:

        aaa

        bb

        ccc\;dd\;eee\\

        ff\\

        ggg\\\;hhh

     

    Thanks!

     


    -Sylvain
    Friday, August 20, 2010 3:22 PM

Answers

  • You can use a zero-width negative lookbehind assertion:

    string input = @"aaa;bb;cccc\;dd\;eee\\;ff\\;ggg\\\;hhh";
    var res = Regex.Split (input, @"(?<!($|[^\\])(\\\\)*?\\);");
    
    

    Looks more complex than it is; the assertion simply prevents the pattern from matching if the semicolon is preceded by an odd number of backslashes (i.e. zero or more pairs of backslashes followed by yet another backslash).

    HTH
    --mc

    Friday, August 20, 2010 4:14 PM

All replies

  • You can use a zero-width negative lookbehind assertion:

    string input = @"aaa;bb;cccc\;dd\;eee\\;ff\\;ggg\\\;hhh";
    var res = Regex.Split (input, @"(?<!($|[^\\])(\\\\)*?\\);");
    
    

    Looks more complex than it is; the assertion simply prevents the pattern from matching if the semicolon is preceded by an odd number of backslashes (i.e. zero or more pairs of backslashes followed by yet another backslash).

    HTH
    --mc

    Friday, August 20, 2010 4:14 PM
  • Works perfectly, thanks!
    -Sylvain
    Friday, August 20, 2010 4:38 PM