Any examples of splitting strings with escaped delimiters?

Looking to use something like String.split – but with the option to define delimiters with potential escape characters.

It appears Elixir’s Regex doesn’t support variable width look behind assertions – so I was wondering if anyone knows some sample code or libraries that might handle this?

Scott S.

So… what, like this?

iex(1)> Regex.split(~R/(?<!\\),/, "comma,separated\\,data,is,here")
["comma", "separated\\,data", "is", "here"]

Or what is it you are looking for?

1 Like

Close, but I also would need the code to allow escaping the escape character as well as many times as needed. So that things like


could include multiple escape characters in the content that, depending on count, could escape the delimiter with an odd number of escape characters or just include them if there are an even number of escapes.

does that make sense? I find escape characters to be difficult to express :frowning:


If you want "comma,separated\\\\\\\\,data,is,here" to parse into ["comma", "separated\\\\\\\\", "data" ,"is" ,"here"] then you cannot do that easily with a proper regex. Something like nimble_parsec is what you want then.

(You ‘can’ do that with a normal regex but it will start getting very hairy-large and you will require a good chunk of external code from the regex to perform the final transformation, where a peg parser can do it all in one swoop.)

1 Like