Extract first N digits from a string

后端 未结 4 960
隐瞒了意图╮
隐瞒了意图╮ 2020-12-18 11:14

I want to extract just FIRST TWO DIGITS from some strings. Suppose the data is :

ABC Conference Room Monitor - Z5580J    
ABC 19 Monitor    
ABC 24 Monitor f         


        
4条回答
  •  萌比男神i
    2020-12-18 12:03

    Solution using regex \\D to match non-digit characters and \\d{2} to match first two digits.

    as.numeric(sub("\\D*(\\d{2}).*", "\\1", INPUT))
    # [1] 55 19 24 24
    

    data:

    INPUT <- c("ABC Conference Room Monitor - Z5580J",
               "ABC 19 Monitor",
               "ABC 24 Monitor for Video-Conferencing",
               "ABC UltraSharp 24 Monitor -QU2482Z")
    

提交回复
热议问题