If we want to extract numbers using - between braces, one option is str_extract . If there are multiple patterns in a string, use str_extract_all
library(stringr) str_extract(str1, '(?<=\\()[0-9-]+(?=\\))')
In the above codes, we use regular expressions to extract numbers and - . A positive lookbehind (?<=\\()[0-9-]+ matches the numbers along with - ( [0-9-]+ ) in (123-456-789 , not 123-456-789 . Similarly, lookahead ('[0-9 -] + (? = \)') Matches numbers together with - in 123-456-789) and not in 123-456-798 . Taken together it matches all cases that satisfy both conditions (123-456-789) , and those that are between the images are extracted, and not in such cases as (123-456-789 or 123-456-789)
With strsplit you can specify split as [()] . We store () in square brackets before [] to treat it as characters, otherwise we need to avoid the brackets ( '\\(|\\)' ).
strsplit(str1, '[()]')[[1]][2] #[1] "123-456-789"
If there are several substrings for extracting from a string, we can execute a loop with lapply and extract the numeric separated parts with grep
lapply(strsplit(str2, '[()]'), function(x) grep('\\d', x, value=TRUE))
Or we can use stri_split from stringi , which can also delete empty lines ( omit_empty=TRUE ).
library(stringi) stri_split_regex(str1, '[()AZ ]', omit_empty=TRUE)[[1]]
Another option is rm_round from qdapRegex if we are interested in extracting the contents inside the brackets.
library(qdapRegex) rm_round(str1, extract=TRUE)[[1]] #[1] "123-456-789" rm_round(str2, extract=TRUE)
data
str1 <- "ABC (123-456-789)" str2 <- c("ABC (123-425-478) A", "ABC(123-423-428)", "(123-423-498) ABCDD", "(123-432-423)", "ABC (123-423-389) GR (124-233-848) AK")