$re = '/^(?<![\s\S\r])(.*)/m';
$str = '# project1
I could have put any other inline tag or string there.
This main body line could have been the very first line as well, if no tags exist.
Change the keys after each `\\n` and add or remove any more keys you want.
In this example, I added the desired tag in the indented multi-line format favoured by Obsidian.
As this is not a Templater solution, `date modified` is hard-coded here for the date you are making your changes. If not needed, it can be removed for the substitution part.
If it is a first time doing regex changes, make a backup of vault or Diary folder.';
$subst = "---\naliases: \ntopic: \ntags: \n - m/diary\npublish: false\ndate modified: 2023-09-21\n---\n\n$1";
$result = preg_replace($re, $subst, $str);
echo "The result of the substitution is ".$result;
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for PHP, please visit: http://php.net/manual/en/ref.pcre.php