R/find_first_regex_line.R

Defines functions find_first_regex_line

Documented in find_first_regex_line

#' Find the first line that satisfies a regex
#' @param lines lines of text
#' @param regex the regex as text
#' @return index of the line
#' @author Richèl J.C. Bilderbeek
#' @export
find_first_regex_line <- function(lines, regex) {
  for (i in seq_along(lines)) {
    match <- stringr::str_extract(
      string = lines[i],
      pattern = regex
    )
    if (!is_one_na(match)) return(i)
  }
  NA
}

Try the beautier package in your browser

Any scripts or data that you put into this service are public.

beautier documentation built on Nov. 2, 2023, 5:08 p.m.