fork download
  1.  
  2. strings <- c("d1 x2 d2x4 5 specimens collected the smallest is 4 x 1 x 3 mm",
  3. " stomach x3 d2x4 8 pieces with the largest measuring 4 x 1 x 3 mm")
  4.  
  5. strings <- gsub("\\b(\\d+)(\\s+(?:specimens|pieces))", ".\\1\\2", strings, perl = TRUE)
  6. strings
Success #stdin #stdout 0.22s 39896KB
stdin
Standard input is empty
stdout
[1] "d1 x2 d2x4 .5 specimens collected the smallest is 4 x 1 x 3 mm"     
[2] " stomach x3 d2x4 .8 pieces with the largest measuring  4 x 1 x 3 mm"