title: R regex for extraction of complex string
body: <p>I have a messy set of strings as follows.</p>
  
  <pre><code>string &lt;- c("GRP-14994/", "GRP-7056 GRP-7036/", "grp-24263(24263)/IRGC 28588", "GRP-15916 /IRGC-42176",
              "GRP-614-250B/", "( GRP 11432)/IRGC-14570", "Tourn", "GRPP256", "Purse", "GRP-14956 Origin:", "GRP 10537", "GRP-10096 Origin: ",
              "SGRP123", "GRP1234", "AC-30009 (GRPHANA)/", "AC-3060 GRP 536-143/Old AC", "RGRPfaa/23", "/-",
              "MGR:7251/", "1216-GR-567/", "X:1 Well KGRPh", "WabGRPvea(II)", "HR33(BGRP)", "Tensor",
              "Wald", "grp12312")
  </code></pre>
  
  <p>I am trying to extract all the instances where GRP followed by digits, which may be separated by space or "-". </p>
  
  <p>My current attempt gives me the following result.</p>
  
  <pre><code>gsub("(.*)(\\b)(GRP)(-|\\s|)(\\d+)(\\/|\\b)(.*)","\\3\\5", string, ignore.case = T)
   [1] "GRP14994"            "GRP7056"             "grp24263"            "GRP15916"           
   [5] "GRP614"              "GRP11432"            "Tourn"               "GRPP256"            
   [9] "Purse"               "GRP14956"            "GRP10537"            "GRP10096"           
  [13] "SGRP123"             "GRP1234"             "AC-30009 (GRPHANA)/" "GRP536"             
  [17] "RGRPfaa/23"          "/-"                  "MGR:7251/"           "1216-GR-567/"       
  [21] "X:1 Well KGRPh"      "WabGRPvea(II)"       "HR33(BGRP)"          "Tensor"             
  [25] "Wald"                "grp12312"      
  </code></pre>
  
  <p>But the desired output ris</p>
  
  <pre><code>out &lt;-  c("GRP14994", "GRP7056 GRP7036", "grp24263", "GRP15916", "GRP614250", 
  "GRP11432", "", "", "", "GRP14956", "GRP10537", "GRP10096", "", 
  "GRP1234", "", "GRP536143", "", "", "", "", "", "", "", "", "", 
  "grp12312")
  
  out
   [1] "GRP14994"        "GRP7056 GRP7036" "grp24263"        "GRP15916"        "GRP614250"       "GRP11432"       
   [7] ""                ""                ""                "GRP14956"        "GRP10537"        "GRP10096"       
  [13] ""                "GRP1234"         ""                "GRP536143"       ""                ""               
  [19] ""                ""                ""                ""                ""                ""               
  [25] ""                "grp12312"    
  </code></pre>
  
  <p>How to modify the regex to get the desired result?</p>