用preg_match替换括号中的引号和单词?

时间:2011-07-17 14:49:00

标签: php regex title movie

我很难以一种奇怪的格式匹配我的几部电影。有些标题用引号括起来,有些标题以#$开头,大部分以结尾的发行年结束(否则为????)。

我正在尝试替换它:

"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

......对此:

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

......如果可能的话,以某种方式获得发行年份。在示例中,我只是放了一个逗号,但是如果你不能得到发布日期就离开它,我会以另一种方式得到它。

我是正则表达式的完全新手,但我还是试着没有运气。如果有人能帮我一把,我真的很感激!


修改

为了减少混乱:

  1. 删除(){}中包含的所有内容。

  2. 删除字符串开头的$#

  3. 如果标题用引号括起来,请将其删除。

  4. 要么保留这样,要么使用某种分组将字符串末尾的发布日期变为单独的变量?

  5. 希望这会有所帮助:)

4 个答案:

答案 0 :(得分:1)

正则表达式应为

$regexp = '[\W]*([\w- üæöä]+)[\W^-].*([\d?]{4})';

答案 1 :(得分:1)

您可以使用此脚本:

<?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";
}
?>

Output

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

答案 2 :(得分:-1)

$string = '"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';

preg_match_all('#(.*?) \(([0-9]+)\)#i', $string, $matches);
$count = count($matches[0]);

for($i = 0; $i < $count; $i++){
    $title = preg_replace('#["\#\$]#us', '', $matches[1][$i]);
    echo "$title, {$matches[2][$i]}"."<br />";
}

结果:

Ein Engel für alle , 2005
Sospecha , 1963
MTV Europe Music Awards 1998 , 1998
Hotel Cæsar , 1998
Am Rande - Sechs Kapitel über AIDS in der Ukraine , 2006

答案 3 :(得分:-2)

试试这个:

$data = '"Ein Engel für alle" (2005) {Katzenjammer (#2.5)}   ????';
$year;
$title;

if (preg_match('#(\d{4})$#', $data, $matches))
{
    $year = $matches[1];
}
if (preg_match('#^(?:"(.*)")|(.*)\s+\(\d{4}\)#', $data, $matches))
{
    $title = ($matches[2] ? $matches[2] : $matches[1]);
}

编辑我的答案以满足您的需求。 ;)

相关问题