2017-02-26 3 views
1

映画のタイトルとリリース年の文字列があります。タイトル(年)のパターンを検出し、一致する場合はアンカータグで囲みたいと思っています。パターンを見つけてアンカータグをラップするPHP正規表現

ラッピングが簡単です。しかし、映画の名前が何であるかわからない場合、このパターンにマッチする正規表現を書くことは可能でしょうか?

例:

$str = 'A random string with movie titles in it. 
Movies like The Thing (1984) and other titles like Captain America Civil War (2016). 
The movies could be anywhere in this string. 
And some movies like 28 Days Later (2002) could start with a number.'; 

ので、パターンは常に(大文字で始まる)Titleなり、(Year)で終了します。これは現在動作しません

if(preg_match('/^\p{Lu}[\w%+\/-]+\([0-9]+\)/', $str)){ 
    error_log('MATCH'); 
} 
else{ 
    error_log('NO MATCH'); 
} 

は、これは私がこれまで持っているものです。私はこれで間違っているつもりです

^\p{Lu} //match a word beginning with an uppercase letter

[\w%+\/-] //with any number of characters following it

+\([0-9]+\) //ending with an integer

:私は、これはそれが何をすべきかであることを理解何から?

+0

'([A-Z] {1} [a-z] + \ s?)+ \(\ d + \)'これはあなたが探しているものです。正規表現のパターンを簡単にテストするには、RegExr(http://regexr.com/)を使用してください。 –

+0

すぐに映画のタイトルが始まるかもしれませんか? '1984(1984)'のように映画が完全な数値であれば?これは何か食料調達する必要がありますか? siamのソリューションは、非常に賢いですが、1984年(1984年)は30歳以上の映画です。私はあなたの提供されたサンプルが起こりうる全ての出来事をカバーしていることを確認したいだけです。 – mickmackusa

+0

一方、totoの正規表現は1984年(1984年)は30歳以上の映画であり、164ステップの節約である。私の拡大サンプルよりも優れているようです。 – mickmackusa

答えて

2

正規表現次それを行う必要があります。

(?-i)(?<=[a-z]\s)[A-Z\d].*?\(\d+\) 

説明

  • (?-i)大文字と小文字を区別任意の小文字とスペース
  • ため
  • (?<=[a-z]\s)見ビハインド
  • 括弧

DEMO

PHP

<?php 
$regex = '/(?-i)(?<=[a-z]\s)[A-Z\d].*?\(\d+\)/'; 
$str = 'A random string with movie titles in it. 
     Movies like The Thing (1984) and other titles like Captain America Civil War (2016). 
     The movies could be anywhere in this string. 
     And some movies like 28 Days Later (2002) could start with a number.'; 
preg_match_all($regex, $str, $matches); 
print_r($matches); 
?> 
+0

大きな回答と説明に感謝します。私は遠く離れていた! –

0

この正規表現を含む一致大文字または数字

  • .*?一致する文字
  • \(\d+\)一致任意の数字行うESの仕事:

    ~(?:[A-Z][a-zA-Z]+\s+|\d+\s+)+\(\d+\)~ 
    

    説明:

    ~    : regex delimiter 
        (?:   : start non capture group 
        [A-Z]  : 1 capital letter, (use \p{Lu} if you want to match title in any language) 
        [a-zA-Z]+ : 1 or more letter, if you want to match title in any language(use \p{L}) 
        \s+   : 1 or more spaces 
        |   : OR 
        \d+   : 1 or more digits 
        \s+   : 1 or more spaces 
    )+   : end group, repeated 1 or more times 
        \(\d+\)  : 1 or more digits surrounded by parenthesis, (use \d{4} if the year is always 4 digits) 
    ~    : regex delimiter 
    

    実装:

    $str = 'A random string with movie titles in it. 
    Movies like The Thing (1984) and other titles like Captain America Civil War (2016). 
    The movies could be anywhere in this string. 
    And some movies like 28 Days Later (2002) could start with a number.'; 
    
    if (preg_match_all('~(?:[A-Z][a-zA-Z]+\s+|\d+\s+)+\(\d+\)~', $str, $match)) { 
        print_r($match); 
        error_log('MATCH'); 
    } 
    else{ 
        error_log('NO MATCH'); 
    } 
    

    結果:

    Array 
    (
        [0] => Array 
         (
          [0] => The Thing (1984) 
          [1] => Captain America Civil War (2016) 
          [2] => 28 Days Later (2002) 
         ) 
    
    ) 
    MATCH 
    
  • 関連する問題