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% patternArguments
- x
A
charactervector 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 inxthere are matches.%~i%: returnsintegerindicating the indices of matches inx.%~n%: returnsintegerindicating the number (count) of matches in each string.%~m%: returnscharacterstring of the matched string itself. ReturnsNAwhere 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.