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
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).