Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

find position of bracket in a string

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 "(".

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading