$re = '/#[NCRLTBUIOSFAGKP]\{(\d+):(#[NCRLTBUIOSFAGKP]\{\d+(?:;F)?\}|S|C|R)(:\d+)?\}/m';
$str = 'Ejemplos Válidos:
Ejemplo 1: #A{4:#S{520}:1}
Ejemplo 2: #A{4:#S{520;F}:2}
Ejemplo 3: #A{4:#S{520}:5}
Ejemplo 4: \'#A{4:#S{520;F}:8}\'!=\'\'
Ejemplo 5: \'#A{4:#S{520}:7}\'!=\'\'
Ejemplo 6: \'#A{4:#S{520;F}:4}\'==\'A\'
Ejemplos Inválidos:
Ejemplo 1: #A{354:#S{500;F}:1:F}
Ejemplo 2: #A{354;#S{500;F};1}
Ejemplo 3: #S{500';
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