The functions give you a concise way to search for regular expressions in character
vectors.
They are "infix" functions, meaning you write the function between its two arguments:
myvector %~% regex
.
Usage
x %~l% regex
x %~i% regex
x %~n% regex
x %~m% regex
x %~% regex
... %!~% NA
... %!~l% NA
x %!~i% pattern
Arguments
- x
A
character
vector to search in.Must be
character
.- regex
One or more regular expressions.
Must be
character
.If more than one regex is supplied, matches to any of the regexes are returned. (See "Multiple regexes" section.)
Details
Each version of the function returns a different type of information about regex matches (if any) in the input vector:
%~l%
: returnslogical
(TRUE
/FALSE
) indicating where inx
there are matches.%~i%
: returnsinteger
indicating the indices of matches inx
.%~n%
: returnsinteger
indicating the number (count) of matches in each string.%~m%
: returnscharacter
string of the matched string itself. ReturnsNA
where there is no match.
The basic function (%~%
) is the same as %~l%
.
There is also a negative versions of the l
and i
functions: giving all
strings that don't match the given regular expression.
These are %!~%
, %!~l%
, and %!~i%
.
These functions are simply syntactic sugar for
existing R
regular expression matching functions:
%~l%
:base::grepl()
%~i%
:base::grep()
Multiple regexes
If more than one regex is supplied,
%~l%
and %~i%
return the indices where any of the regexes match.
In the case of %~n%
, each matching regex is counted separately, and they are all summed.
In the case of %~m%
, all matches (if any) are pasted together,
including multiple matches of the same string.