regex: Pattern matching and Replacement

Description Usage Arguments Details See Also Examples

Description

These functions provide simple extensions to base regular expressions in R, primarily intended to assist with extraction of elements based on the result of a regular expression evaluation.

Usage

 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
ngrep(pattern, x, ignore.case = FALSE, perl = FALSE, value = FALSE,
  fixed = FALSE, useBytes = FALSE)

fgrep(pattern, x, ignore.case = FALSE, value = FALSE, useBytes = FALSE,
  invert = FALSE)

re_exists(x, pattern, perl = TRUE, fixed = FALSE, ...)

re.exists(pattern, x, perl = TRUE, fixed = FALSE, ...)

re_extract(x, pattern, perl = TRUE, fixed = FALSE, ...)

extract.re(x, pattern, perl = TRUE, fixed = FALSE, ...)

re_without(x, pattern, perl = TRUE, fixed = FALSE, ...)

without.re(x, pattern, perl = TRUE, fixed = FALSE, ...)

re_extract_rows(x, pattern, match_var = rownames(x), perl = TRUE,
  fixed = FALSE, ...)

extract_rows.re(x, pattern, match_var = rownames(x), perl = TRUE,
  fixed = FALSE, ...)

re_without_rows(x, pattern, match_var = rownames(x), perl = TRUE,
  fixed = FALSE, ...)

without_rows.re(x, pattern, match_var = rownames(x), perl = TRUE,
  fixed = FALSE, ...)

Arguments

x

An R object (in the case of re_ methods), or a character vector (in the case of ngrep, fgrep)

pattern

character string containing a character string to be matched in the given character vector; coerced by as.character if possible.

ignore.case

boolean; if TRUE we perform case-insensitive matching.

perl

boolean; if TRUE, we use perl-compatible regular expressions.

value

boolean; if TRUE we return the actual matches; if FALSE we return the indices corresponding to the matches.

fixed

boolean; if TRUE the pattern is matched as-is. Overrides all conflicting arguments.

useBytes

boolean; if TRUE we perform matching byte-by-byte rather than character by character.

match_var

A variable to match on, as used in the re_extract_rows function.

invert

Invert the results of the regular expression match?

...

Optional arguments passed to grep or grepl.

Details

The order is reversed for the re_ set of functions; i.e., an R object is expected first, rather than a regular expression pattern.

See Also

grep

grep, regex

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
ngrep( "abc", c("abc", "babcd", "abcdef", "apple"), value=TRUE )
if( re_exists(c("apple", "banana"), "^ap") ) print("yay!")
dat <- data.frame( x=letters, y=LETTERS )
rownames(dat) <- 1:26
## get all rows in dat with a 1, 2, 3 or 4 in the name
re_extract_rows( dat, "[1-4]" )
dat <- data.frame( x=letters, y=LETTERS )
rownames(dat) <- 1:26
## get all rows in dat with a 1, 2, 3 or 4 in the name
re_without_rows( dat, "[0-4]" )

Kmisc documentation built on May 29, 2017, 1:43 p.m.

Related to regex in Kmisc...