$re = '/\\\\RequirePackage(\[([^][]*)\])?([^{]*){([^{}]*)}.*(?:\n\s*\[([^][]*)])?/m';
$str = '\\RequirePackage[
top = 2.5cm,
bottom = 2.5cm,
left = 2.5cm,
right = 2.5cm,
marginparwidth = 2cm,
marginparsep = 2mm,
heightrounded
]{geometry}%
[2020-01-02]
\\RequirePackage{tocbasic}
\\RequirePackage[svgnames]%
{xcolor}%
[2023/11/15]
\\RequirePackage[raggedright]% OK?
{titlesec}
\\RequirePackage{xcolor}%
[2022/06/12]
\\RequirePackage{hyperref}% To load after titlesec!
[2023-02-07]';
preg_match_all($re, $str, $matches, PREG_SET_ORDER, 0);
// Print the entire match result
var_dump($matches);
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