operators (version 0.1-8)

patternSubstitution: Remove a pattern from a character vector

Description

Removes a pattern from a character vector.

Usage

txt %-~% pattern txt %-~|% pattern txt %o~|% pattern

Arguments

txt
text to manipulate
pattern
regular expression

Value

%-~% : Removes the pattern rx from the character vector x. It is equivalent of using gsub( rx, "", x ).%-~|% does a two-step operation. First, it selects the elements of x that match the pattern rx and then it removes the pattern from the rest.%o~|% does a slightly more complicated two-step operation. It first gets the elements of txt that match the pattern and then keeps only the part that matches the pattern. Similar to the grep -o in recent versions of unix.

See Also

grep, gsub

Examples

Run this code
  txt <- c("arm","foot","lefroo", "bafoobar")
  txt %-~% "foo"
  txt %-~|% "foo"
  
  ### Email of the R core team members
  rcore <- readLines(file.path(R.home("doc"),"AUTHORS")) 
  rcore 
  
  ### or this way
  # angle brackets are retained here
  rcore %o~|% "<.*@.*>"
  rcore %o~|% "<.*@.*>" %-~% "[<>]"
  
  
  # allows to perform the match using < and > but strips them from the result
  rcore %o~|% "<(.*@.*)>"
  
  # really silly english to french translator
  pinks <- colors() %~|% "pink"
  pinks %s~% "/pink/rose/"
  gsub( "pink", "rose", pinks )
  
  # perl regex pink shouter
  pinks %s~% "/(pink)/\\U\\1/p"
  gsub( "(pink)", "\\U\\1", pinks, perl = TRUE )

  # see ?gsub
  gsub("(\\w)(\\w*)", "\\U\\1\\L\\2", "a test of capitalizing", perl=TRUE)
  "a test of capitalizing" %s~% "/(\\w)(\\w*)/\\U\\1\\L\\2/gp"
  

Run the code above in your browser using DataLab