acter strings or character vectors are coerced
to character if possible.

Each of these functions operates in one of three modes:

  1. ` fixed = TRUE ` : use exact matching. 

  2. ` perl = TRUE ` : use Perl-style regular expressions. 

  3. ` fixed = FALSE, perl = FALSE ` : use POSIX 1003.2 extended regular expressions (the default). 

See the help pages on [ regular expression
](/link/regular%20expression?package=base&version=3.6.2) for details of the
different types of regular expressions.

The two ` *sub ` functions differ only in that ` sub ` replaces only the first
occurrence of a ` pattern ` whereas ` gsub ` replaces all occurrences. If `
replacement ` contains backreferences which are not defined in ` pattern ` the
result is undefined (but most often the backreference is taken to be ` "" ` ).

For ` regexpr ` , ` gregexpr ` and ` regexec ` it is an error for ` pattern `
to be ` NA ` , otherwise ` NA ` is permitted and gives an ` NA ` match.

The main effect of ` useBytes ` is to avoid errors/warnings about invalid
inputs and spurious matches in multibyte locales, but for ` regexpr ` it
changes the interpretation of the output. It inhibits the conversion of inputs
with marked encodings, and is forced if any input is found which is marked as
` "bytes" ` (see ` [ Encoding ](/link/Encoding?package=base&version=3.6.2) `
).

Caseless matching does not make much sense for bytes in a multibyte locale,
and you should expect it only to work for ASCII characters if ` useBytes =
TRUE ` .

` regexpr ` and ` gregexpr ` with ` perl = TRUE ` allow Python-style named
captures, but not for _long vector_ inputs.

Invalid inputs in the current locale are warned about up to 5 times.

Caseless matching with ` perl = TRUE ` for non-ASCII characters depends on the
PCRE library being compiled with ‘Unicode property support’.

##  References

Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988) _The New S Language_ .
Wadsworth & Brooks/Cole ( ` grep ` )

##  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.

` [ regmatches ](/link/regmatches?package=base&version=3.6.2) ` for extracting
matched substrings based on the results of ` regexpr ` , ` gregexpr ` and `
regexec ` .

` [ glob2rx ](/link/glob2rx?package=base&version=3.6.2) ` to turn wildcard
matches into regular expressions.

` [ agrep ](/link/agrep?package=base&version=3.6.2) ` for approximate
matching.

` [ charmatch ](/link/charmatch?package=base&version=3.6.2) ` , ` [ pmatch
](/link/pmatch?package=base&version=3.6.2) ` for partial matching, ` [ match
](/link/match?package=base&version=3.6.2) ` for matching to whole strings, ` [
startsWith ](/link/startsWith?package=base&version=3.6.2) ` for matching of
initial parts of strings.

` [ tolower ](/link/tolower?package=base&version=3.6.2) ` , ` [ toupper
](/link/toupper?package=base&version=3.6.2) ` and ` [ chartr
](/link/chartr?package=base&version=3.6.2) ` for character translations.

` [ apropos ](/link/apropos?package=base&version=3.6.2) ` uses regexps and has
more examples.

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

Options ` PCRE_limit_recursion ` , ` PCRE_study ` and ` PCRE_use_JIT ` .

` [ extSoftVersion ](/link/extSoftVersion?package=base&version=3.6.2) ` for
the versions of regex and PCRE libraries in use, ` [ pcre_config
](/link/pcre_config?package=base&version=3.6.2) ` for more details for PCRE.

##  Examples

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

    
    
    # NOT RUN {
    grep("[a-z]", letters)
    
    txt <- c("arm","foot","lefroo", "bafoobar")
    if(length(i <- grep("foo", txt)))
       cat("'foo' appears at least once in\n\t", txt, "\n")
    i # 2 and