How the hell to split multiple delims?

mipri mipri at minimaltype.com
Sat Feb 15 11:43:48 UTC 2020


On Saturday, 15 February 2020 at 11:32:42 UTC, AlphaPurned wrote:
> I've tried 10 different ways with split and splitter, I've used 
> all the stuff that people have said online but nothing works. I 
> always get a template mismatch error.
>
> Why is something so easy to do so hard in D?
>
> auto toks = std.regex.split(l, Regex("s"));
> auto toks = std.regex.splitter(l, Regex("s"));
> auto toks = std.regex.splitter(l, ctRegex!r"\.");

Why do you think this doesn't work? I'm not just asking this to
be rude. Your problem is likely in the rest of your code.

$ rdmd --eval 'split("the quick,brown:fox",regex("[ 
,:]")).writeln'
["the", "quick", "brown", "fox"]

$ rdmd --eval 'typeid(split("the quick,brown:fox",regex("[ 
,:]"))).writeln'
immutable(char)[][]

$ rdmd --eval 'splitter("the quick,brown:fox",regex("[ 
,:]")).writeln'
["the", "quick", "brown", "fox"]

$ rdmd --eval 'typeid(splitter("the quick,brown:fox",regex("[ 
,:]"))).writeln'
std.regex.Splitter!(cast(Flag)false, string, Regex!char).Splitter

That final value is a range type. Consider the guides
at the top of https://dlang.org/phobos/std_range.html
Ranges take a while to get comfortable with.


More information about the Digitalmars-d-learn mailing list