re_convenience {mclm} | R Documentation |
Convenience functions in support of regular expressions
Description
These functions are essentially simple wrappers around base R functions such as
regexpr()
, gregexpr()
, grepl()
, grep()
, sub()
and gsub()
.
The most important differences between the functions documented here and the
R base functions is the order of the arguments (x
before pattern
) and the
fact that the argument perl
is set to TRUE
by default.
Usage
re_retrieve_first(
x,
pattern,
ignore.case = FALSE,
perl = TRUE,
fixed = FALSE,
useBytes = FALSE,
requested_group = NULL,
drop_NA = FALSE,
...
)
re_retrieve_last(
x,
pattern,
ignore.case = FALSE,
perl = TRUE,
fixed = FALSE,
useBytes = FALSE,
requested_group = NULL,
drop_NA = FALSE,
...
)
re_retrieve_all(
x,
pattern,
ignore.case = FALSE,
perl = TRUE,
fixed = FALSE,
useBytes = FALSE,
requested_group = NULL,
unlist = TRUE,
...
)
re_has_matches(
x,
pattern,
ignore.case = FALSE,
perl = TRUE,
fixed = FALSE,
useBytes = FALSE,
...
)
re_which(
x,
pattern,
ignore.case = FALSE,
perl = TRUE,
fixed = FALSE,
useBytes = FALSE,
...
)
re_replace_first(
x,
pattern,
replacement,
ignore.case = FALSE,
perl = TRUE,
fixed = FALSE,
useBytes = FALSE,
...
)
re_replace_all(
x,
pattern,
replacement,
ignore.case = FALSE,
perl = TRUE,
fixed = FALSE,
useBytes = FALSE,
...
)
Arguments
x |
Character vector to be searched or modified. |
pattern |
Regular expression specifying what is to be searched. |
ignore.case |
Logical. Should the search be case insensitive? |
perl |
Logical. Whether the regular expressions use the PCRE flavor
of regular expression. Unlike in base R functions, the default is |
fixed |
Logical. If |
useBytes |
Logical. If |
requested_group |
Numeric.
If |
drop_NA |
Logical. If |
... |
Additional arguments. |
unlist |
Logical. If |
replacement |
Character vector of length one specifying the replacement
string. It is to be taken literally, except that the notation |
Details
For some of the arguments (e.g. perl
, fixed
) the reader is directed to
base R's regex documentation.
Value
re_retrieve_first()
, re_retrieve_last()
and re_retrieve_all()
return
either a single vector of character data or a list containing such vectors.
re_replace_first()
and re_replace_all()
return the same type of character
vector as x
.
re_has_matches()
returns a logical vector indicating whether a match was
found in each of the elements in x
; re_which()
returns a numeric
vector indicating the indices of the elements of x
for which a match was
found.
Functions
-
re_retrieve_first()
: Retrieve from each item inx
the first match ofpattern
. -
re_retrieve_last()
: Retrieve from each item inx
the last match ofpattern
. -
re_retrieve_all()
: Retrieve from each item inx
all matches ofpattern
. -
re_has_matches()
: Simple wrapper aroundgrepl()
. -
re_which()
: Simple wrapper aroundgrep()
. -
re_replace_first()
: Simple wrapper aroundsub()
. -
re_replace_all()
: Simple wrapper aroundgsub()
.
Examples
x <- tokenize("This is a sentence with a couple of words in it.")
pattern <- "[oe](.)(.)"
re_retrieve_first(x, pattern)
re_retrieve_first(x, pattern, drop_NA = TRUE)
re_retrieve_first(x, pattern, requested_group = 1)
re_retrieve_first(x, pattern, drop_NA = TRUE, requested_group = 1)
re_retrieve_first(x, pattern, requested_group = 2)
re_retrieve_last(x, pattern)
re_retrieve_last(x, pattern, drop_NA = TRUE)
re_retrieve_last(x, pattern, requested_group = 1)
re_retrieve_last(x, pattern, drop_NA = TRUE, requested_group = 1)
re_retrieve_last(x, pattern, requested_group = 2)
re_retrieve_all(x, pattern)
re_retrieve_all(x, pattern, unlist = FALSE)
re_retrieve_all(x, pattern, requested_group = 1)
re_retrieve_all(x, pattern, unlist = FALSE, requested_group = 1)
re_retrieve_all(x, pattern, requested_group = 2)
re_replace_first(x, "([oe].)", "{\\1}")
re_replace_all(x, "([oe].)", "{\\1}")