how to extract the first number from each string in a vector in R?

后端 未结 7 1146
醉话见心
醉话见心 2020-12-18 10:53

I am new to regex in R. Here I have a vector where I am interested in extracting the first occurance of a number in each string of the vector .

I have a vector calle

相关标签:
7条回答
  • 2020-12-18 11:25

    You can use sub:

    test <- ("xff 34 sfsdg 352 efsrg")
    
    sub(".*?(\\d+).*", "\\1", test)
    # [1] "34"
    

    How does the regex work?

    The . matches any character. The quantifier * means any number of occurrences. The ? is used to match all characters up to the first match of \\d (digits). The quantifier + means one or multiple occurrences. The brackets around \\d are the first match group. This may be followed by additional characters (.*). The second argument (\\1) replaces the whole string with the first match group (i.e., the first number).

    0 讨论(0)
提交回复
热议问题