library(stringr)
string <- string <- c("pat1 hello333\n pat2 ok i mean pat1 again pat2 some more text pat1")

I want to match all strings that start with pat1 and end with pat2.
> str_extract_all(
  string,
  regex(
    "pat1.+pat2",
    dotall=TRUE
  )
)
[[1]]
[1] "pat1 hello333\n pat2 ok i mean pat1 again pat2"

This gives me 1 string that starts with pat1 and ends with pat2. However, my desired output is something like:
> output
[1] "pat1 hello333\n pat2"
[2] "pat1 again pat2"


How to match multiple occurrences of strings given a start and end pattern in R?
Change .+ to .+? to have non greedy match.
library(stringr)
str_extract_all(string, regex("pat1.+?pat2", dotall=TRUE))[[1]]
#[1] "pat1 hello333\n pat2" "pat1 again pat2" 

You can use gregexpr and regmatches with pat1.*?pat2 or in case they should be on a word boundary with \\bpat1\\b.*?\\bpat2\\b. Where .*? matches everything but minimal.
regmatches(string, gregexpr("pat1.*?pat2", string))[[1]]
#[1] "pat1 hello333\n pat2" "pat1 again pat2"     

regmatches(string, gregexpr("\\bpat1\\b.*?\\bpat2\\b", string))[[1]]
#[1] "pat1 hello333\n pat2" "pat1 again pat2"     

