Using regular expressions when reading a file
forkit
forkit at gmail.com
Fri May 6 05:40:52 UTC 2022
On Thursday, 5 May 2022 at 17:53:57 UTC, Alexander Zhirov wrote:
> I want to use a configuration file with external settings. I'm
> trying to use regular expressions to read the `Property =
> Value` settings. I would like to do it all more beautifully. Is
> there any way to get rid of the line break character? How much
> does everything look "right"?
regex never looks right ;-)
try something else perhaps??
// ------------
module test;
import std;
void main()
{
auto file = File("d:\\settings.conf", "r");
string[string] aa;
// create an associate array of settings -> [key:value]
foreach (line; file.byLine().filter!(a => !a.empty))
{
auto myTuple = line.split(" = ");
aa[myTuple[0].to!string] = myTuple[1].to!string;
}
// write out all the settings.
foreach (key, value; aa.byPair)
writefln("%s:%s", key, value);
writeln;
// write just the host value
writeln(aa["host"]);
}
// ------------
More information about the Digitalmars-d-learn
mailing list