Using regular expressions when reading a file
Alexander Zhirov
azhirov1991 at gmail.com
Thu May 5 20:14:04 UTC 2022
On Thursday, 5 May 2022 at 19:19:26 UTC, Ali Çehreli wrote:
> Couldn't help myself from improving. :) The following regex
> works in my Linux console. No issues with '\n'. (?) It also
> allows for leading and trailing spaces:
>
> import std.regex;
> import std.stdio;
> import std.algorithm;
> import std.array;
> import std.typecons;
> import std.functional;
>
> void main() {
> auto p_property = regex(r"^ *(\w+) *= *(\w+) *$");
> const properties = File("settings.conf")
> .byLineCopy
> .map!(line => matchFirst(line, p_property))
> .filter!(not!empty) // OR: .filter!(m =>
> !m.empty)
> .map!(m => tuple(m[1], m[2]))
> .assocArray;
>
> writeln(properties);
> }
It will need to be sorted out with a fresh head. 😀 Thanks!
More information about the Digitalmars-d-learn
mailing list