title: What is the mistake in this two lines of code? Getting pattern: "http:// blabla .nc"
body: <p>I have hundreds of TXT files which contain many things and some download links.</p>
  
  <p>The pattern of the download links are like this:</p>
  
  <p><strong>start with:</strong> http://</p>
  
  <p>and </p>
  
  <p><strong>end with:</strong> .nc</p>
  
  <p>I created a sample text file for your convenience that you could download from this link: </p>
  
  <p><a href="https://www.dropbox.com/s/5crmleli2ppa1rm/textfile_including_https.txt?dl=1" rel="nofollow noreferrer">https://www.dropbox.com/s/5crmleli2ppa1rm/textfile_including_https.txt?dl=1</a></p>
  
  <p>Based on this topic in Stackoverflow, I tried to extract all download links from the text file:</p>
  
  <p><a href="https://stackoverflow.com/questions/15579119/extract-websites-links-from-a-text-in-r">Extract websites links from a text in R</a></p>
  
  <p><strong>Here is my code:</strong></p>
  
  <pre><code>download_links &lt;- readLines(file.choose())
  All_my_links &lt;- gsub(download_links, pattern=".*(http://.*nc).*", replace="\\1")
  </code></pre>
  
  <p>But it returns all lines, too, <strong>while I only want to extract the http links ended with .nc</strong></p>
  
  <p>Here is the result:</p>
  
  <pre><code>head(All_my_links )
  tail(All_my_links )
  
  &gt; head(All_my_links )
  
  [1] "#!/bin/bash"                                                                                                                                                                              
  [2] "##############################################################################"                                                                                                           
  [3] "version=1.3.2"                                                                                                                                                                            
  [4] "CACHE_FILE=.$(basename $0).status"                                                                                                                                                        
  [5] "openId="                                                                                                                                                                                  
  [6] "search_url='https://esgf-node.llnl.gov/esg-search/wget/?distrib=false&amp;dataset_id=CMIP6.HighResMIP.MIROC.NICAM16-9S.highresSST-present.r1i1p1f1.day.pr.gr.v20190830|esgf-data2.diasjp.net'"
  
  &gt; tail(All_my_links )
  
  [1] "MYPROXY_STATUS=$HOME/.MyProxyLogon"                                 
  [2] "COOKIE_JAR=$ESG_HOME/cookies"                                       
  [3] "MYPROXY_GETCERT=$ESG_HOME/getcert.jar"                              
  [4] "CERT_EXPIRATION_WARNING=$((60 * 60 * 8))   #Eight hour (in seconds)"
  [5] ""                                                                   
  [6] "WGET_TRUSTED_CERTIFICATES=$ESG_HOME/certificates"   
  </code></pre>
  
  <p><strong>What is my mistake in the code?</strong></p>
  
  <p>Any comment would be highly appreciated.</p>