digitalmars.D.learn - Regex split ignoore empty and whitespace
- AlphaPurned (5/5) Feb 20 2020 std.regex.split(l, ctRegex!`[\s-\)\(\.]`);
- =?UTF-8?Q?Ali_=c3=87ehreli?= (21/25) Feb 20 2020 It turns out, split uses splitter, which is more capable, like allowing
- =?UTF-8?Q?Ali_=c3=87ehreli?= (6/8) Feb 20 2020 After realizing that No.keepSeparators is the default value anyway, I=20
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
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
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