fork download
  1. library(stringr)
  2. parts <- c("6 4- to 5-mm-diameter plugs", #1 6 4- to...diameter
  3. "6 large bricks", #2 6 large
  4. "1 1/3 shipment concrete", #3 1.33 shipment
  5. "1 (14- to 15-oz) gold bars", #4 1 (14- to 15-oz)
  6. "16 3 1/2- to 4-inch-diameter caps", #5 16 3 1/2- to...eter
  7. "1 1/2 tons sand", #6 1.5 tons
  8. "2 1 1/4- to 3-inch diameter caps", #7 2 1 1/4- to...eter
  9. "1/3 shipment cement")
  10.  
  11. m <- str_match(parts, '^(\\d+(?:\\s+\\d+/\\d+)?|\\d+/\\d+)\\s+((?:\\d+(?:-?in(?:ch)?|")?\\s+)*\\S+(?:\\s+to\\s+(?:\\d+(?:-?in(?:ch)?|")?\\s+)*\\S+)?)')
  12. qty <- m[,2]
  13. unit <- m[,3]
  14. qty
  15. unit
Success #stdin #stdout 0.33s 41984KB
stdin
Standard input is empty
stdout
[1] "6"     "6"     "1 1/3" "1"     "16"    "1 1/2" "2"     "1/3"  
[1] "4- to 5-mm-diameter"       "large"                    
[3] "shipment"                  "(14- to 15-oz)"           
[5] "3 1/2- to 4-inch-diameter" "tons"                     
[7] "1 1/4- to 3-inch diameter" "shipment"