Regex split ignoore empty and whitespace
Ali Çehreli
acehreli at yahoo.com
Fri Feb 21 00:46:19 UTC 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
More information about the Digitalmars-d-learn
mailing list