patternSubstitution: Remove a pattern from a character vector

Description Usage Arguments Value Note Author(s) See Also Examples

Description

Removes a pattern from a character vector.

Usage

1
2
3
4
  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.

Note

%-~% does the substitution via the gsub function. One can pass arguments to the gsub function using the operators.gsub option declared by this package. See %but% for a description of this mechanism.

The filtering in %-~|% is performed by %~|% and therefore options can be passed to regexpr using the operators.regexpr option.

For %o~|%, if the pattern given does not contain opening and closing round brackets, the entire matching space is retained, otherwise only the part that is contained between the brackets is retained, see the example below.

%s~% is an attempt to provide some of the functionnality of the unix's sed. The pattern is split by "/" and used as follows: the first part is the regular expression to replace, the second is the replacement, and the (optional) third gives modifiers to the gsub function used to perform the replacement. Modifiers are passed to gsub with the %but% operator. The "g" modifier can also be used in order to control if the gsub function is used for global replacement or the sub function to only replace the first match. At the moment "/" cannot be used in the regular expressions.

Author(s)

Romain Francois <francoisromain@free.fr>

See Also

grep, gsub

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
                        
  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"
  

operators documentation built on May 2, 2019, 6:48 p.m.