title: Split parts of string defined by multiple delimiters into multiple variables in R
body: <p>I have a large list of file names that I need to extract information from using R. The info is delimited by multiple dashes and underscores. I am having trouble figuring out a method that will accommodate the fact that the number of characters between delimiters is not consistent (the order of the information will remain constant, as will the delimiters used (hopefully)). </p>
  
  <p>For example:</p>
  
  <pre><code> f &lt;- data.frame(c("EI-SM4-AMW11_20160614_082800.wav", "PA-RF-A50_20160614_082800.wav"), stringsAsFactors = FALSE)
   colnames(f)&lt;-"filename"
   f$area &lt;- str_sub(f$filename, 1, 2)
   f$rec &lt;- str_sub(f$filename, 4, 6)
   f$site &lt;- str_sub(f$filename, 8, 12)
  </code></pre>
  
  <p>This produces correct results for the first file, but incorrect results for the second. </p>
  
  <p>I've tried using the "stringr" and "stringi" packages, and know that hard coding the values in doesn't work, so I've come up with awkward solutions using both packages such as:</p>
  
  <pre><code>f$site &lt;- str_sub(f$filename, 
                    stri_locate_last(f$filename, fixed="-")[,1]+1, 
                    stri_locate_first(f$filename, fixed="_")[,1]-1)
  </code></pre>
  
  <p>I feel like there must be a more elegant (and robust) method, perhaps involving regex (which I am painfully new to). </p>
  
  <p>I've looked at other examples (<a href="https://stackoverflow.com/questions/29752250/extract-part-of-string-till-the-first-semicolon-in-r">Extract part of string (till the first semicolon) in R</a>, <a href="https://stackoverflow.com/questions/5214677/r-find-the-last-dot-in-a-string">R: Find the last dot in a string</a>, <a href="https://stackoverflow.com/questions/27050718/split-string-using-regular-expressions-and-store-it-into-data-frame">Split string using regular expressions and store it into data frame</a>).</p>
  
  <p>Any suggestions/pointers would be very much appreciated.</p>