str_line_replace.RdThese functions serve to change the first or last line of strings which match a specific pattern (regex). `str_firstLine_replace()` replaces the first line that matches the pattern. `str_lastLine_replace()` replaces the last line that matches the pattern They are useful, for example, when reading a text file with many lines and you want to preserve the lines of that text file. When `which='all'`, it is a wrapper for `stringr::str_replace()`.
These functions serve to change the first or last line of strings which match a specific pattern (regex). `str_firstLine_replace()` replaces the first line that matches the pattern. `str_lastLine_replace()` replaces the last line that matches the pattern They are useful, for example, when reading a text file with many lines and you want to preserve the lines of that text file. When `which='all'`, it is a wrapper for `stringr::str_replace()`.
str_line_replace(
str,
pattern,
replacement,
which = c("first", "last", "poles", "all")
)
str_firstLine_replace(str, pattern, replacement)
str_lastLine_replace(str, pattern, replacement)
str_line_replace(
str,
pattern,
replacement,
which = c("first", "last", "poles", "all")
)
str_firstLine_replace(str, pattern, replacement)
str_lastLine_replace(str, pattern, replacement)String with pattern to make replacement
Regular expression to replace
What to replace the pattern with
which one? first, last, all or the poles (first AND last)
string
string
[stringr::str_replace()]
[stringr::str_replace()]
somelines <- c('AAAAA', 'textytext', 'BBBBB', 'AAAAA', 'writingwriting', 'AAAAA', 'etc', 'etc', 'BBBBB')
str_firstLine_replace(somelines, 'AAAAA', 'changedfirstline')
#> [1] "changedfirstline" "textytext" "BBBBB" "AAAAA"
#> [5] "writingwriting" "AAAAA" "etc" "etc"
#> [9] "BBBBB"
str_lastLine_replace(somelines, 'AAAAA', 'changedlastline')
#> [1] "AAAAA" "textytext" "BBBBB" "AAAAA"
#> [5] "writingwriting" "changedlastline" "etc" "etc"
#> [9] "BBBBB"
str_line_replace(somelines, 'AAAAA', 'changedpoles', which='poles')
#> [1] "changedpoles" "textytext" "BBBBB" "AAAAA"
#> [5] "writingwriting" "changedpoles" "etc" "etc"
#> [9] "BBBBB"
str_line_replace(somelines, 'AAAAA', 'changedall', which='all')
#> [1] "changedall" "textytext" "BBBBB" "changedall"
#> [5] "writingwriting" "changedall" "etc" "etc"
#> [9] "BBBBB"
somelines <- c('AAAAA', 'textytext', 'BBBBB', 'AAAAA', 'writingwriting', 'AAAAA', 'etc', 'etc', 'BBBBB')
str_firstLine_replace(somelines, 'AAAAA', 'changedfirstline')
#> [1] "changedfirstline" "textytext" "BBBBB" "AAAAA"
#> [5] "writingwriting" "AAAAA" "etc" "etc"
#> [9] "BBBBB"
str_lastLine_replace(somelines, 'AAAAA', 'changedlastline')
#> [1] "AAAAA" "textytext" "BBBBB" "AAAAA"
#> [5] "writingwriting" "changedlastline" "etc" "etc"
#> [9] "BBBBB"
str_line_replace(somelines, 'AAAAA', 'changedpoles', which='poles')
#> [1] "changedpoles" "textytext" "BBBBB" "AAAAA"
#> [5] "writingwriting" "changedpoles" "etc" "etc"
#> [9] "BBBBB"
str_line_replace(somelines, 'AAAAA', 'changedall', which='all')
#> [1] "changedall" "textytext" "BBBBB" "changedall"
#> [5] "writingwriting" "changedall" "etc" "etc"
#> [9] "BBBBB"