Thread overview
Regex split ignoore empty and whitespace
Feb 20, 2020
AlphaPurned
Feb 21, 2020
Ali Çehreli
Feb 21, 2020
Ali Çehreli
February 20, 2020
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?
February 20, 2020
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

February 20, 2020
On 2/20/20 4:46 PM, Ali Çehreli wrote:

>    auto range = std.regex.splitter!(No.keepSeparators)(l,
> ctRegex!`[\s-\)\(\.]+`);

After realizing that No.keepSeparators is the default value anyway, I tried 'split' and it worked the way you wanted. So, perhaps all you needed was that extra '+' in the regex pattern:

  std.regex.split(l, ctRegex!`[\s-\)\(\.]+`)

Ali