<?php
$regex = <<<REGEX
~
^ # start of string anchor
(CONF|ESD|TRACKING) # start capture group 1 KEY, three literal words
(?: # start non-capturing group 1
\h*[:;'\h]\h* # require a listed punctuation or space with optional leading or trailing spaces
(\S+(?:\h+\S+)*?) # start capture group 2 LINE, require one or more non-whitespace characters then lazily match zero or more repetitions of whitespace then non-whitespace substrings
(?: # start non-capturing group 2
\h*L\h*[:;'\h]\h* # require literal L then a listed punctuation or space with optional leading or trailing spaces
( # start capture group 3 LINE_DATA
(?:\d+(?:\(\d+\))?) # require a number optionally followed by another number in parentheses
(?:\h*,\h*\d+(?:\(\d+\))?)* # optionally match zero or more repetitions of the previous expression if separated by an optionally space-padded comma
) # end capture group 3 and make it optional
)? # end non-capturing group 2
(?: # start non-capturing group 3
\h* # match zero or more whitespaces
( # start capture group 4 INITIALS
\*[.a-z]+ # match literal asterisk, then one or more dots and letters
) # end capture group 4
)? # end non-capturing group 3 and make it optional
)? # end non-capturing group 2 and make it optional
\h* # allow trailing whitespaces
$ # end of string anchor
~ix
REGEX;
$tests = [
"esd hedf L:1,2,3 *sm ",
"CONF: FEDEX 12345 L: 12(2),2(9),32 *SM",
"Tracking *cool",
"ESD: 12/12/92 L: ",
"tRacking' my data L: 1,2,3(4) ",
"conf something *asterisk",
"tracking",
"ConF''' something '' L: 6",
"esd test 24(7)",
];
foreach ($tests as $i => $test) {
if (preg_match($regex, $test, $m, PREG_UNMATCHED_AS_NULL)) {
var_export([
"test index" => $i,
"KEY" => $m[1],
"LINE" => $m[2] ?? null,
"LINE_DATA" => $m[3] ?? null,
"INITIALS" => $m[4] ?? null
]);
echo "\n";
}
}