####################################################################################################
#
# Functions for working with strings
#
# @author ConradStack <conrad.stack@gmail.com>
#
.REGEX_parenthesis = "\\([^\\(\\)]{0,}\\)"
.REGEX_bracketed = "\\[[^\\[\\]]{0,}\\]"
.REGEX_curlies = "\\{[^\\{\\}]{0,}\\}"
#' Remove 'annotation' sections from strings
#'
#' @param x character vector
#' @param replacement replace matched pattern with this
#' @param paren match parenthetical annotations
#' @param square match square-bracketed annotations
#' @param curly match curly-bracketed annotations
#' @param ... passed to `stringr::regex`
#'
#' @return modified vector with annotation blocks removed
#' @importFrom stringr str_remove_all str_extract_all str_replace_all regex str_detect
#' @export
remove_annotations <- function(x, paren=FALSE, square=FALSE, curly=FALSE, ...) {
if( paren ) x = str_remove_all(x, regex(.REGEX_parenthesis, ...) )
if( square ) x = str_remove_all(x, regex(.REGEX_bracketed, ...) )
if( curly ) x = str_remove_all(x, regex(.REGEX_curlies, ...) )
return(x)
}
#' @describeIn remove_annotations remove all parenthetical phrases
#' @export
remove_paren <- function(x, ...){
return(remove_annotations(x, paren=TRUE, ...))
}
#' @describeIn remove_annotations remove all curly-bracketed phrases
#' @export
remove_curly <- function(x, ...){
return(remove_annotations(x, curly=TRUE, ...))
}
#' @describeIn remove_annotations remove all square-bracketed phrases
#' @export
remove_square <- function(x, ...){
return(remove_annotations(x, square=TRUE, ...))
}
#' @describeIn remove_annotations replace text annotations
#' @export
replace_annotations <- function(x, replacement, paren=FALSE, square=FALSE, curly=FALSE, ...) {
if( paren ) x = str_replace_all(x, regex(.REGEX_parenthesis, ...), replacement )
if( square ) x = str_replace_all(x, regex(.REGEX_bracketed, ...), replacement )
if( curly ) x = str_replace_all(x, regex(.REGEX_curlies, ...), replacement )
return(x)
}
#' @describeIn remove_annotations replace all parenthetical phrases
#' @export
replace_paren <- function(x, replacement, ...){
return(replace_annotations(x, replacement, paren=TRUE, ...))
}
#' @describeIn remove_annotations replace all curly-bracketed phrases
#' @export
replace_curly <- function(x, replacement,...){
return(replace_annotations(x, replacement, curly=TRUE, ...))
}
#' @describeIn remove_annotations replace all square-bracketed phrases
#' @export
replace_square <- function(x, replacement, ...){
return(replace_annotations(x, replacement, square=TRUE, ...))
}
#' @describeIn remove_annotations extract all parenthetical annotations
#' @export
extract_parentheses <- function(x, ...) {
str_extract_all(x, regex(.REGEX_parenthesis, ...) )
}
#' @describeIn remove_annotations extract all square-bracketed annotations
#' @export
extract_square <- function(x, ...) {
str_extract_all(x, regex(.REGEX_bracketed, ...) )
}
#' @describeIn remove_annotations extract all curly-bracketed annotations
#' @export
extract_curly <- function(x, ...) {
str_extract_all(x, regex(.REGEX_curlies, ...) )
}
#' @describeIn remove_annotations detect parenthetical annotations
#' @export
detect_parentheses <- function(x, ...) {
str_detect(x, regex(.REGEX_parenthesis, ...) )
}
#' @describeIn remove_annotations detect square-bracketed annotations
#' @export
detect_square <- function(x, ...) {
str_detect(x, regex(.REGEX_bracketed, ...) )
}
#' @describeIn remove_annotations detect curly-bracketed annotations
#' @export
detect_curly <- function(x, ...) {
str_detect(x, regex(.REGEX_curlies, ...) )
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.