3v4l.org

run code in 300+ PHP versions simultaneously
<?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"; } }
Output for 7.4.0 - 7.4.30, 8.0.1 - 8.0.22, 8.1.0 - 8.1.9
array ( 'test index' => 0, 'KEY' => 'esd', 'LINE' => 'hedf', 'LINE_DATA' => '1,2,3', 'INITIALS' => '*sm', ) array ( 'test index' => 1, 'KEY' => 'CONF', 'LINE' => 'FEDEX 12345', 'LINE_DATA' => '12(2),2(9),32', 'INITIALS' => '*SM', ) array ( 'test index' => 2, 'KEY' => 'Tracking', 'LINE' => '*cool', 'LINE_DATA' => NULL, 'INITIALS' => NULL, ) array ( 'test index' => 3, 'KEY' => 'ESD', 'LINE' => '12/12/92 L:', 'LINE_DATA' => NULL, 'INITIALS' => NULL, ) array ( 'test index' => 4, 'KEY' => 'tRacking', 'LINE' => 'my data', 'LINE_DATA' => '1,2,3(4)', 'INITIALS' => NULL, ) array ( 'test index' => 5, 'KEY' => 'conf', 'LINE' => 'something', 'LINE_DATA' => NULL, 'INITIALS' => '*asterisk', ) array ( 'test index' => 6, 'KEY' => 'tracking', 'LINE' => NULL, 'LINE_DATA' => NULL, 'INITIALS' => NULL, ) array ( 'test index' => 7, 'KEY' => 'ConF', 'LINE' => '\'\' something \'\'', 'LINE_DATA' => '6', 'INITIALS' => NULL, ) array ( 'test index' => 8, 'KEY' => 'esd', 'LINE' => 'test 24(7)', 'LINE_DATA' => NULL, 'INITIALS' => NULL, )

preferences:
59.76 ms | 420 KiB | 14 Q