grapes-help-twiddle-grapes: Infix regular expression match operator with side effects.

Description Usage Arguments Details Value Note See Also Examples

Description

If there is a match, returns TRUE and also assigns the match result to a variable in the current environment ('.match' by default).

Usage

1
lhs %?~% rhs

Arguments

lhs

the string to be matched

rhs

a regular expression string or RegularExpression object (obtained from 'get.regexp' or 'RE').

Details

Note: if lhs is not a string, this method tries to coerce it to one.

Value

logical

Note

due to the way scoping works in R, the value of the match variable will be overwritten upon each use of this operator. To perform nested use of this operator, you need to place each within a 'local()' block.

See Also

local

Examples

1
2
3
4
5
# prints 'a'; .match is set in the calling environment
if ('a' %?~% '([ab])') print(.match$group(1))

# prints 'a'; .match is *not* set in the calling environment
local(if ('a' %?~% '([ab])') print(.match$group(1))) # prints 'a';

jdidion/LikeThis documentation built on May 18, 2019, 11:30 p.m.