title: pattern matching with sub(), unable to catch and replace first occurrence
body: <p>The followings are the results I expect </p>
  
  <pre><code>&gt; title = "La La Land (2016/I)"
  [1]"(2016" #result
  &gt; title = "_The African Americans: Many Rivers to Cross with Henry Louis Gates, Jr._ (2013) _The Black Atlantic (1500-1800) (#1.1)_"
  [1]"(2013" #result
  &gt; title = "dfajfj(2015)asdfjuwer f(2017)fa.erewr6"
  [1]"(2015" #result
  </code></pre>
  
  <p>==================================================================</p>
  
  <p>The followings are what I got by applying code<code>sub(pattern=".*(\\(\\d{4}.*\\)).*", title, replacement="\\1")</code></p>
  
  <pre><code>&gt; title = "_The African Americans: Many Rivers to Cross with Henry Louis Gates, Jr._ (2013) _The Black Atlantic (1500-1800) (#1.1)_"
  &gt; sub(pattern=".*(\\(\\d{4}.*\\)).*", title, replacement="\\1")
  [1] "(1500-1800) (#1.1)"  #result. However, I expected it to be "(2013)"
  &gt; title = "La La Land (2016/I)"
  &gt; sub(pattern=".*(\\(\\d{4}.*\\)).*", title, replacement="\\1")
  [1] "(2016/I)" #result as I expect
  &gt; title = "dfajfj(2015)asdfjuwer f(2017)fa.erewr6"
  &gt; sub(pattern=".*(\\(\\d{4}.*\\)).*", title, replacement="\\1")
  [1]"(2017)" # result. However, I expect it to be "（2015)"
  </code></pre>
  
  <p>The followings are what I GOT by applying code<code>sub(pattern=".*(\\(\\d{4}\\)).*", title, replacement="\\1")</code></p>
  
  <pre><code>&gt; title = "La La Land (2016/I)"
  &gt; sub(pattern=".*(\\(\\d{4}\\)).*", title, replacement="\\1")
  [1] "La La Land (2016/I)" #result. However, I expect it to be "(2016)"
  &gt; title = "dfajfj(2015)asdfjuwer f(2017)fa.erewr6" 
  &gt; sub(pattern=".*(\\(\\d{4}\\)).*", title, replacement="\\1")
  [1] "(2017)" #result. However, I expect it to be "(2015)"
  &gt; title = "_The African Americans: Many Rivers to Cross with Henry Louis Gates, Jr._ (2013) _The Black Atlantic (1500-1800) (#1.1)_"
  &gt; sub(pattern=".*(\\(\\d{4}\\)).*", title, replacement="\\1")
  [1] "(2013)" #result as I expect
  </code></pre>
  
  <p>I checked the description of <code>sub</code>, it says "sub performs replacement of the first match. In this case, the first match should be <code>(2013)</code>.</p>
  
  <p>In a word, I try to write a <code>sub()</code>command to return the first occurrence of a year in a string. </p>
  
  <p>I guess there is something wrong with my code but couldn't find it, appreciate if anyone could help me. </p>
  
  <p>==================================================================</p>
  
  <p>In fact, my ultimate goal is to extract the year of all movies. However, I don't know how to do it in one step. Therefore, I decide to first find the year in <code>(dddd</code> format, then use code <code>sub(pattern="\\((\\d{4}).*", a, replacement="\\1")</code> to find the pure number of the year. </p>
  
  <p>for example:</p>
  
  <pre><code>&gt; a= "(2015"
  &gt; sub(pattern="\\((\\d{4}).*", a, replacement="\\1")
  [1] "2015"
  &gt; a= "(2015)"
  &gt; sub(pattern="\\((\\d{4}).*", a, replacement="\\1")
  [1] "2015"
  </code></pre>
  
  <p>=================<strong>updated 05/29/2017 22:51PM</strong>=======================</p>
  
  <p>the <code>str_extract</code> in akrun's answer works well with my dataset. </p>
  
  <p>However, the <code>sub()</code> doesn't work for all data. The following are what I did. However, my code doesn't work with all 500 records. I would really appreciate if anyone could point out the mistakes on my codes. I really cannot figure it out myself. Thank you very much.</p>
  
  <pre><code>&gt; t1
  [1] "Man Who Fell to Earth (Remix) (2010) (TV)"
  &gt; t2
  [1] "Manual pr\u0087ctico del amigo imaginario (abreviado) (2008)"
  &gt; title = c(t1,t2)
  &gt; x=gsub(pattern=".*(\\(\\d{4}.*\\)).*", title, replacement="\\1")
  &gt; x
  [1] "(2010) (TV)" "(2008)"     
  &gt; sub(pattern="\\((.*)\\).*", x, replacement="\\1")
  [1] "2010) (TV" "2008"     
  </code></pre>
  
  <p>However, my goal is to get <code>2010</code> and <code>2008</code>. My code works with <code>t2</code> but fails with <code>t1</code></p>