replace_text() is the in-memory counterpart of replace_files(). It applies
the replacements stored in a seekr_match object to text and returns the
modified text.
It does not read files, write files, or create backups. Use replace_files()
for the usual file-based workflow.
Arguments
- text
Text content as a single string.
- x
A
seekr_matchobject with replacement values. All matches inxmust be associated with the same file and must refer to positions intext.
Details
replace_text() verifies that the current text has the same hash as the text
that was searched when the matches were created. If the text has changed,
replacement is considered unsafe and the function aborts.
Matches are replaced from the end of the file to the beginning, so earlier replacements do not shift the recorded positions of later replacements.
replace_text() requires x to contain matches from a single source, the one
corresponding to text.
See also
match_text()to create matches from already-read text.replace_files()to apply replacements directly to files.filter_match()to keep only some matches before replacing.
Examples
text <- "hello old_name\nbye old_name"
x <- match_text(
text = text,
path = "example.txt",
pattern = "old_name",
replacement = "new_name"
)
replace_text(text, x)
#> [1] "hello new_name\nbye new_name"
