$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.
If it is a first time doing regex changes, make a backup of vault or Diary folder.';
$subst = "---\ntags: \n - m/diary\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