# Here is a string with three patterns of digits
text <- "cars123ruin456cities789"
# To extract the first one we can use this pattern
stringr::str_extract(text, "\\d{3}")
# or
create_lookaround("\\d{3}", "cars", "before") |>
stringr::str_extract(string=text)
# To exclude the first one we can write
create_lookaround("\\d{3}", "cars", "before", negate=TRUE) |>
stringr::str_extract_all(string=text)
# To extract the second one we can write
create_lookaround("\\d{3}", "ruin", "before") |>
stringr::str_extract(string=text)
# or
create_lookaround("\\d{3}", "cities", "after") |>
stringr::str_extract(string=text)
Run the code above in your browser using DataLab