Note that there are some explanatory texts on larger screens.

plurals
  1. POR:how to get grep to return the match, rather than the whole string
    text
    copied!<p>I have what is probably a really dumb <code>grep</code> in R question. Apologies, because this seems like it should be so easy - I'm obviously just missing something. </p> <p>I have a vector of strings, let's call it <code>alice</code>. Some of <code>alice</code> is printed out below:</p> <pre><code>T.8EFF.SP.OT1.D5.VSVOVA#4 T.8EFF.SP.OT1.D6.LISOVA#1 T.8EFF.SP.OT1.D6.LISOVA#2 T.8EFF.SP.OT1.D6.LISOVA#3 T.8EFF.SP.OT1.D6.VSVOVA#4 T.8EFF.SP.OT1.D8.VSVOVA#3 T.8EFF.SP.OT1.D8.VSVOVA#4 T.8MEM.SP#1 T.8MEM.SP#3 T.8MEM.SP.OT1.D106.VSVOVA#2 T.8MEM.SP.OT1.D45.LISOVA#1 T.8MEM.SP.OT1.D45.LISOVA#3 </code></pre> <p>I'd like grep to give me the number after the D that appears in some of these strings, conditional on the string containing "LIS" and an empty string or something otherwise. </p> <p>I was hoping that grep would return me the value of a capturing group rather than the whole string. Here's my R-flavoured regexp:</p> <pre><code>pattern &lt;- (?&lt;=\\.D)([0-9]+)(?=.LIS) </code></pre> <p>nothing too complicated. But in order to get what I'm after, rather than just using <code>grep(pattern, alice, value = TRUE, perl = TRUE)</code> I'm doing the following, which seems bad:</p> <pre><code>reg.out &lt;- regexpr( "(?&lt;=\\.D)[0-9]+(?=.LIS)", alice, perl=TRUE ) substr(alice,reg.out,reg.out + attr(reg.out,"match.length")-1) </code></pre> <p>Looking at it now it doesn't seem too ugly, but the amount of messing about it's taken to get this utterly trivial thing working has been embarrassing. Anyone any pointers about how to go about this properly?</p> <p>Bonus marks for pointing me to a webpage that explains the difference between whatever I access with <code>$</code>,<code>@</code> and <code>attr</code>.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload