[ RDocumentation ](/)

Moon  [ ](https://github.com/datacamp/rdocumentation-2.0) [ Learn R
](https://www.datacamp.com/learn/r)

Search all packages and functions

[ base  (version  3.6.2  ) ](/packages/base/versions/3.6.2)

#  grepRaw: Pattern Matching for Raw Vectors

##  Description

` grepRaw ` searches for substring ` pattern ` matches within a raw vector ` x
` .

##  Usage

    
    
    grepRaw(pattern, x, offset = 1L, ignore.case = FALSE,
            value = FALSE, fixed = FALSE, all = FALSE, invert = FALSE)

##  Arguments

pattern

raw vector containing a [ regular expression
](/link/regular%20expression?package=base&version=3.6.2) (or fixed pattern for
` fixed = TRUE ` ) to be matched in the given raw vector. Coerced by ` [
charToRaw ](/link/charToRaw?package=base&version=3.6.2) ` to a character
string if possible.

x

a raw vector where matches are sought, or an object which can be coerced by `
charToRaw ` to a raw vector. [ Long vectors
](/link/Long%20vectors?package=base&version=3.6.2) are not supported.

ignore.case

if ` FALSE ` , the pattern matching is _case sensitive_ and if ` TRUE ` , case
is ignored during matching.

offset

An integer specifying the offset from which the search should start. Must be
positive. The beginning of line is defined to be at that offset so ` "^" `
will match there.

value

logical. Determines the return value: see ‘Value’.

fixed

logical. If ` TRUE ` , ` pattern ` is a pattern to be matched as is.

all

logical. If ` TRUE ` all matches are returned, otherwise just the first one.

invert

logical. If ` TRUE ` return indices or values for elements that do _not_
match. Ignored (with a warning) unless ` value = TRUE ` .

##  Value

` grepRaw(value = FALSE) ` returns an integer vector of the offsets at which
matches have occurred. If ` all = FALSE ` then it will be either of length
zero (no match) or length one (first matching position).

` grepRaw(value = TRUE, all = FALSE) ` returns a raw vector which is either
empty (no match) or the matched part of ` x ` .

` grepRaw(value = TRUE, all = TRUE) ` returns a (potentially empty) list of
raw vectors corresponding to the matched parts.

##  Details

Unlike ` [ grep ](/link/grep?package=base&version=3.6.2) ` , seeks matching
patterns within the raw vector ` x ` . This has implications especially in the
` all = TRUE ` case, e.g., patterns matching empty strings are inherently
infinite and thus may lead to unexpected results.

The argument ` invert ` is interpreted as asking to return the complement of
the match, which is only meaningful for ` value = TRUE ` . Argument ` offset `
determines the start of the search, not of the complement. Note that ` invert
= TRUE ` with ` all = TRUE ` will split ` x ` into pieces delimited by the
pattern including leading and trailing empty strings (consequently the use of
regular expressions with ` "^" ` or ` "$" ` in that case may lead to less
intuitive results).

Some combinations of arguments such as ` fixed = TRUE ` with ` value = TRUE `
are supported but are less meaningful.

##  See Also

[ regular expression ](/link/regular%20expression?package=base&version=3.6.2)
(aka ` [ regexp ](/link/regexp?package=base&version=3.6.2) ` ) for the details
of the pattern specification.

` [ grep ](/link/grep?package=base&version=3.6.2) ` for matching character
vectors.

##  Examples

[ Run this code
](https://app.datacamp.com/workspace/preview?_tag=rdocs&rdocsPath=packages%2Fbase%2Fversions%2F3.6.2%2Ftopics%2FgrepRaw&utm_source=r-docs&utm_medium=docs&utm_term=grepRaw&utm_content=run_example_in_workspace)

    
    
    # NOT RUN {
    grepRaw("no match", "textText")  # integer(0): no match
    grepRaw("adf", "adadfadfdfadadf") # 3 - the first match
    grepRaw("adf", "adadfadfdfadadf", all=TRUE, fixed=TRUE)
    ## [1]  3  6 13 -- three matches
    # }
    

Run the code above in your browser using  [ DataCamp Workspace
](https://app.datacamp.com/workspace/preview?_tag=rdocs&rdocsPath=packages%2Fbase%2Fversions%2F3.