Retain unmatched strings with regmatches (R)
<p><strong>Background Problem:</strong></p>

<p>Currently we have a list of item descriptions (1278 obs), some of which have Product Codes in them. E.g ("REF VLOC0004 V LOC 3/0 15CM SUTURE DEVICE"), the code would be VLOC0004.</p>

<p>I have this stored as an excel file, and I would like a column next to the description column with the item codes filled in next to each description.</p>

<p><strong>Current Work done:</strong></p>

<p>I have managed to load the data into R and am able to write my own patterns for regular expressions (I may revist this if I feel I am not up to the task of picking up a few strings of codes). E.g R Code below. </p>

<pre><code># Pattern
p=("([A-Z]*[0-9]{4,}[A-Z]*)|([0-9]+[:punct:][0-9]+)|([0-9]+[:punct:][0-9]+[:punct:][0-9]+)")
# Match data from regexpr()
m&lt;-regexpr(p,EGIA$LGY_DHB_ITEM_DESCRIPTION)
</code></pre>

<p><strong>The Problem:</strong></p>

<p>When I run regmatches to extract the pattern above, it only extracts found matches and ignores descriptions with no codes in them. The result is it creates a vector of only 608 length. If I were to use this column next to my item descriptions, the codes wouldn't line up with the descriptions. What I need is this vector to somehow give a blank spot where the item code isn't found, so that this extract vector is the same length of the item description. </p>

<p>I played around with the inverse command but it did no good. Any help on this would be appreciated, I have looked around numerous tutorials on regular expressions and on this site but am overwhelmed with information, none of which seems relevant. </p>