Вы можете использовать этот скрипт:
<?php
$inputs = Array(
'"Ein Engel für alle" (2005) {Katzenjammer (#2.5)} ????',
'#"Sospecha" (1963) {El caso del viejo del Tibet} 1963',
'MTV Europe Music Awards 1998 (1998) (TV) 1998',
'"Hotel Cæsar" (1998) {(#12.26)} 1998',
'$Am Rande - Sechs Kapitel über AIDS in der Ukraine (2006) 2006'
);
foreach ($inputs as $input) {
$matches = Array();
if (!preg_match('/^(?:\$|#)?(?:"(.+?)"|(.+?)) \(\d{4}\) .* (\d{4}|\?{4})$/', $input, $matches))
continue;
print $matches[1] . $matches[2] . ", " . $matches[3] . "\n";
}
?>
Вывод :
Ein Engel für alle, ????
Sospecha, 1963
MTV Europe Music Awards 1998, 1998
Hotel Cæsar, 1998
Am Rande - Sechs Kapitel über AIDS in der Ukraine, 2006
Это должно точно и точно соответствовать вашим заданным правилам (хотя он не используетваши предложенные методологические шаги, которые на самом деле не соответствуют решению сопоставления с образцом).
Давайте более подробно рассмотрим это регулярное выражение:
/ # start of regex
^ # starting delimiter and start-of-input
(?:\$|#)? # $ or # (but don't capture)
(?: # (don't capture the outer group)
"(.+?)"|(.+?) # title either in quotes or not
)
#\(\d{4}\) # the inner date (delimits the title when the title has no quotes)
.* # any other inner fluff
(\d{4}|\?{4}) # either four digits, or four question marks
$ # the end-of-input must immediately follow
/ # end of regex