3v4l.org

run code in 300+ PHP versions simultaneously
<?php $pattern = "/^([A-Z\d]+)\[(?|(\d+)-(\d+)|([A-Z])-([A-Z]))]|([A-Z\d]+)$/"; $strings = [ "SW[1-3]", "LD1[A-D]", "LD1" ]; foreach ($strings as $s) { if (preg_match($pattern, $s, $match)) { if (array_key_exists(4, $match)) { echo $match[4] . PHP_EOL; continue; } foreach (range($match[2], $match[3]) as $m) { echo $match[1] . $m . PHP_EOL; } } }

preferences:
25.56 ms | 405 KiB | 5 Q