split_by_pattern() allow you to split a character vector considering a
start and end pattern.
Usage
split_by_pattern(
x,
start_pattern = "^# |^## |---",
end_pattern = NULL,
name_list = TRUE,
include_start = FALSE
)Arguments
- x
A
charactervector.- start_pattern
(optional) A string with the start pattern (default:
"^# |^## |---").- end_pattern
(optional) A string with the end pattern. Use
NULLif there isn't one (default:NULL).- name_list
(optional) A
logicalflag indicating if each output item should have a name (default:TRUE).- include_start
(optional) A
logicalflag 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 vector functions:
cut_vector(),
remove_caps(),
replace_caps()
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"
#>