Search code examples
stringsplitd

Want to separate a string by spaces, but not escaped spaces


I would like to write some D code that would take a string, and split it by " ", but not "\ ". I normally use std.array.split for splitting, but it obviously can't handle this. What would be the best way to do what I want?


Solution

  • Regular expressions (+ lookbehind) is powerful enough for that:

    import std.regex;
    void main()
    {
        auto parts = split(r"foo bar\ bar baz", regex(r"(?<!\\) "));
        assert(parts == ["foo", r"bar\ bar", "baz"]);
    }
    

    http://dlang.org/phobos/std_regex.html