title: capture IP addresses only using R
body: <p>I have R objects that have domain names and IP addresses in them. For example.</p>
  
  <pre><code>11.22.44.55.test.url.com.localhost
  </code></pre>
  
  <p>I used regex in R to capture the IP addresses. My problem is that when there is no match the whole string gets matched or "outputed". This becomes a problem as I work on a very large dataset. I currently have the following using regex</p>
  
  <pre><code>sub("([0-9]+)\\.([0-9]+)\\.([0-9]+)\\.([0-9]+).*","\\1.\\2.\\3.\\4","11.22.44.55.test.url.com.localhost")
  </code></pre>
  
  <p>which gives me 11.22.44.55 </p>
  
  <pre><code>11.22.44.55
  </code></pre>
  
  <p>but if I were to have to following</p>
  
  <pre><code>sub("([0-9]+)\\.([0-9]+)\\.([0-9]+)\\.([0-9]+).*","\\1.\\2.\\3.\\4","11.22.44.test.url.com.localhost")
  </code></pre>
  
  <p>Then it gives me </p>
  
  <pre><code>11.22.44.test.url.com.localhost
  </code></pre>
  
  <p>which is actually not correct. Wondering if there is any solution for this.</p>