patternFilter: Regular expression filters

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

Description

Filters a character vector by a regular expression.

Usage

1
2
3
  x %~|% rx
  x %!~|% rx
  

Arguments

x

text to manipulate

rx

regular expression

Value

'%~|%' : a character vector containing all the elements of x that match the regular expression rx or NULL if there is no match.

'%!~|%' : a character vector containing all the elements of x that do not match the regular expression rx.

Note

The filtering is done using the regexpr function. Logical arguments of regexpr can be indirectly used by %~|% or %!~|% by using the operators.regexpr option declared with this package. See %but% for a description of this mecanism.

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
                        
  
  cols <- colors()
  cols %~|% "^blue"
  
  ### blue colors that don't finish with a digit
  ( a1 <- cols %~|% "blue" %!~|% "\\d$"         )
  ( a2 <- cols %~|% "blue[^0-9]*$"                )
  ( a3 <- grep( "blue[^0-9]*", cols, value = TRUE ) )
  
  # using perl regular expressions
  
  ### not necessary since p is in the default of the package
  with( options( operators.regexpr = "p" ), { 
  	( a4 <- grep( "blue[^\\d]*", cols, value = TRUE, perl = TRUE  ) )
  	( a5 <- cols %~|% "blue[^\\d]*$" )
  })
  
  ### blue colors that contain a r
  cols %~|% "blue" %~|% "r"
  grep( "r", grep( "blue", cols, value = TRUE ), value = TRUE  )
  
  ### blue colors that don't contain a r
  cols %~|% "blue" %!~|% "r"
  cols %~|% "^[^r]*blue[^r]*$"
  
  grep( "^[^r]*$", grep( "blue", cols, value = TRUE ), value = TRUE  ) # tricky and verbose
  # or in two steps, ... laborious
  bluecols <- grep( "blue", cols, value = TRUE )
  bluecols[ -grep( "r", bluecols) ]
  

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