I’m looking to find the position of a bracket within a string.
mystring <- "VAR_c(1:9)_XYZ"
I’d like to find the position of "(".
>Solution :
You could use gregexpr to find your bracket (Note you should add \\ to find the bracket) with unlist, if you have multiple brackets it will show all the positions, like this:
mystring <- "VAR_c(1:9)_XYZ"
unlist(gregexpr('\\(', mystring))
#> [1] 6
Example with another bracket to show it will give you all the positions like this:
mystring2 <- "VAR_c(1:9)_XYZ("
unlist(gregexpr('\\(', mystring2))
#> [1] 6 15
Created on 2023-02-16 with reprex v2.0.2