On 2/20/20 2:02 PM, AlphaPurned wrote:> std.regex.split(l, ctRegex!`[\s-\)\(\.]`);
>
> I'm trying too split a string on spaces and stuff... but it is returning
> empty strings and other matches(e.g., ()).
>
> I realize I can delete afterwards but is there a direct way from split
> or ctRegex?

It turns out, split uses splitter, which is more capable, like allowing to say "do not keep the separators". The difference is, splitter returns a range, so I called .array to give you an array but it's not necessary.

I took liberty to add a '+' to your pattern, which may not be useful in your case:

import std.regex;
import std.stdio;
import std.typecons : No, Yes;
import std.array;

void main() {
  auto l = "hello world\t  and  moon";
auto range = std.regex.splitter!(No.keepSeparators)(l, ctRegex!`[\s-\)\(\.]+`);
  auto array = range.array;

  writeln(range);
  writeln(array);
}

Both lines print ["hello", "world", "and", "moon"].

Ali

Reply via email to