regexpr
returns an integer vector of the same length as
text
giving the starting position of the first match or
-1 if there is none, with attribute "match.length"
, an
integer vector giving the length of the matched text (or -1 for
no match). The match positions and lengths are in characters unless
useBytes = TRUE
is used, when they are in bytes.
regexpr(pattern, text, ignore.case = FALSE, perl = FALSE, fixed = FALSE, useBytes = FALSE)
• pattern
: regular expression, or string for fixed=TRUE
• text
: string, the character vector
• ignore.case
: case sensitive or not
• perl
: logical. Should perl-compatible regexps be used? Has priority over extended
• fixed
: logical. If TRUE, pattern is a string to be matched as is. Overrides all conflicting arguments
• useBytes
: logical. If TRUE the matching is done byte-by-byte rather than character-by-character
> x <- "line 4322: He is now 25 years old, and weights 130lbs" > y <- regexpr("\\d+",x) > y
[1] 6 attr(,"match.length") [1] 4 attr(,"useBytes") [1] TRUE
> x <- "line 4322: He is now 25 years old, and weights 130lbs" > y <- regexpr("[[:digit:]]",x) > y
[1] 6 attr(,"match.length") [1] 1 attr(,"useBytes") [1] TRUE
> if (y[[1]][1] != -1) print("match")
[1] "match"
Vector match:
>str <- c("Regular", "expression", "examples of R language") >x <- regexpr("x*ress",str) >x
[1] -1 4 -1
Regular Expression Syntax: