www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Regex split ignoore empty and whitespace

reply AlphaPurned <Alpha Beta.com> writes:
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?
Feb 20 2020
parent reply =?UTF-8?Q?Ali_=c3=87ehreli?= <acehreli yahoo.com> writes:
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
Feb 20 2020
parent =?UTF-8?Q?Ali_=c3=87ehreli?= <acehreli yahoo.com> writes:
On 2/20/20 4:46 PM, Ali =C3=87ehreli wrote:

    auto range =3D std.regex.splitter!(No.keepSeparators)(l,
 ctRegex!`[\s-\)\(\.]+`);
After realizing that No.keepSeparators is the default value anyway, I=20 tried 'split' and it worked the way you wanted. So, perhaps all you=20 needed was that extra '+' in the regex pattern: std.regex.split(l, ctRegex!`[\s-\)\(\.]+`) Ali
Feb 20 2020