Extract numeric part of strings of mixed numbers and characters in R

后端 未结 4 613
傲寒
傲寒 2020-11-28 07:22

I have a lot of strings, and each of which tends to have the following format: Ab_Cd-001234.txt I want to replace it with 001234. How can I achieve

4条回答
  •  北荒
    北荒 (楼主)
    2020-11-28 08:05

    Using gsub or sub you can do this :

     gsub('.*-([0-9]+).*','\\1','Ab_Cd-001234.txt')
    "001234"
    

    you can use regexpr with regmatches

    m <- gregexpr('[0-9]+','Ab_Cd-001234.txt')
    regmatches('Ab_Cd-001234.txt',m)
    "001234"
    

    EDIT the 2 methods are vectorized and works for a vector of strings.

    x <- c('Ab_Cd-001234.txt','Ab_Cd-001234.txt')
    sub('.*-([0-9]+).*','\\1',x)
    "001234" "001234"
    
     m <- gregexpr('[0-9]+',x)
    > regmatches(x,m)
    [[1]]
    [1] "001234"
    
    [[2]]
    [1] "001234"
    

提交回复
热议问题