Getting started with regular expressions, the very basics

library(stringr)

# standard patterns
blank <- ""
newline <- "\\n"
period <- "\\."

# customizable to reuse the same function argument below
to_trim <- ";.*$"
snip <- "an inherent"
swap_from <- "potentates"
swap_to <- "oligarchs"
older <- "and"
newer <- "plus"

# sample text
some_text <- "The potentates     of this     world are sufficiently apt to consider themselves as possessed of an inherent superiority which gives them a right to govern and makes mankind their own; and this infatuation is almost every where fostered in them by the creeping sycophants about them and the language of flattery which they are continually hearing."
some_text
#> [1] "The potentates     of this     world are sufficiently apt to consider themselves as possessed of an inherent superiority which gives them a right to govern and makes mankind their own; and this infatuation is almost every where fostered in them by the creeping sycophants about them and the language of flattery which they are continually hearing."

# remove everything in the to_trim variable to the end

str_remove(some_text,to_trim)
#> [1] "The potentates     of this     world are sufficiently apt to consider themselves as possessed of an inherent superiority which gives them a right to govern and makes mankind their own"

# remove first occurence

str_remove(some_text, older)
#> [1] "The potentates     of this     world are sufficiently apt to consider themselves as possessed of an inherent superiority which gives them a right to govern  makes mankind their own; and this infatuation is almost every where fostered in them by the creeping sycophants about them and the language of flattery which they are continually hearing."

# remove first occurence
str_remove_all(some_text, older)
#> [1] "The potentates     of this     world are sufficiently apt to consider themselves as possessed of an inherent superiority which gives them a right to govern  makes mankind their own;  this infatuation is almost every where fostered in them by the creeping sycophants about them  the language of flattery which they are continually hearing."

# vanilla search and replace the first occurrence
str_replace(some_text,swap_from,swap_to)
#> [1] "The oligarchs     of this     world are sufficiently apt to consider themselves as possessed of an inherent superiority which gives them a right to govern and makes mankind their own; and this infatuation is almost every where fostered in them by the creeping sycophants about them and the language of flattery which they are continually hearing."

# search and replace all occurrences
str_replace_all(some_text, older, newer)
#> [1] "The potentates     of this     world are sufficiently apt to consider themselves as possessed of an inherent superiority which gives them a right to govern plus makes mankind their own; plus this infatuation is almost every where fostered in them by the creeping sycophants about them plus the language of flattery which they are continually hearing."

# close up extra white space
str_squish(some_text)
#> [1] "The potentates of this world are sufficiently apt to consider themselves as possessed of an inherent superiority which gives them a right to govern and makes mankind their own; and this infatuation is almost every where fostered in them by the creeping sycophants about them and the language of flattery which they are continually hearing."

Created on 2020-03-20 by the reprex package (v0.3.0)

3 Likes

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.