How to extract everything until first occurrence of pattern : <p>I'm trying to use the <a href="https://github.com/tidyverse/stringr" rel="noreferrer">stringr</a> package in R to extract everything from a string up until the first occurrence of an underscore.</p>

<p><strong>What I've tried</strong></p>

<pre><code>str_extract("L0_123_abc", ".+?(?&lt;=_)")
&gt; "L0_"
</code></pre>

<p>Close but no cigar.  How do I get this one?  Also, Ideally I'd like something that's easy to extend so that I can get the information in between the 1st and 2nd underscore and get the information after the 3rd underscore.</p>
