Using regular expressions when reading a file

H. S. Teoh hsteoh at quickfur.ath.cx
Thu May 5 18:15:28 UTC 2022


On Thu, May 05, 2022 at 05:53:57PM +0000, Alexander Zhirov via Digitalmars-d-learn 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"?
[...]
> ```d
> auto file = File("settings.conf", "r");
> string[string] properties;
> auto p_property = regex(r"^\w+ *= *.+", "s");
> while (!file.eof())
> {
>   string line = file.readln();
>   auto m = matchAll(line, p_property);
>   if (!m.empty())
>   {
>     string property = matchAll(line, regex(r"^\w+", "m")).hit;
>     string value = replaceAll(line, regex(r"^\w+ *= *", "m"), "");
>     properties[property] = value;
>   }
> }

Your regex already matches the `Property = Value` pattern; why not just
use captures to extract the relevant parts of the match, insteead of
doing it all over again inside the if-statement?

	// I added captures (parentheses) to extract the property name
	// and value directly from the pattern.
	auto p_property = regex(r"^(\w+) *= *(.+)", "s");

	// I assume you only want one `Property = Value` pair per input
	// line, so you really don't need matchAll; matchFirst will do
	// the job.
	auto m = matchFirst(line, p_property);

	if (m) {
		// No need to run a match again, just extract the
		// captures
		string property = m[1];
		string value = m[2];
		properties[property] = value;
	}


T

-- 
"You are a very disagreeable person." "NO."


More information about the Digitalmars-d-learn mailing list