[Experimental]

split_by_pattern() allow you to split a character vector considering a start and end pattern.

split_by_pattern(
  x,
  start_pattern = "^# |^## |---",
  end_pattern = NULL,
  name_list = TRUE,
  include_start = FALSE
)

Arguments

x

A character vector.

start_pattern

A string with the start pattern (default: "^# |^## |---").

end_pattern

A string with the end pattern. Use NULL if there isn't one (default: NULL).

name_list

A logical flag indicating if each output item should have a name (default: TRUE).

include_start

A logical flag indicating if the line flagged in the start pattern should be included in the split (default: FALSE).

Value

A list with the split character vector.

See also

Other string functions: cutter(), replace_pattern()

Examples

x <- c(
 "# Title 1",
 "## Subtitle 1",
 "Content 1",
 "Content 2",
 "# Title 2",
 "## Subtitle 2",
 "Content 3",
 "Content 4"
 )

 split_by_pattern(
   x,
   start_pattern = "^# |^## |---",
   end_pattern = NULL,
   name_list = TRUE,
   include_start = FALSE
 )
#> $`# Title 1 [line 1]`
#> character(0)
#> 
#> $`## Subtitle 1 [line 2]`
#> [1] "Content 1" "Content 2"
#> 
#> $`# Title 2 [line 5]`
#> character(0)
#> 
#> $`## Subtitle 2 [line 6]`
#> [1] "Content 3" "Content 4"
#>