[ RDocumentation ](/)

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

Search all packages and functions

[ micropan  (version  1.0  ) ](/packages/micropan/versions/1.0)

#  gregexpr: Extended ` [ gregexpr
](/link/gregexpr?package=micropan&version=1.0) ` with substring retrieval

##  Description

An extension of the base function ` [ gregexpr
](/link/gregexpr?package=micropan&version=1.0) ` enabling retrieval of the
matching substrings.

##  Usage

    
    
    gregexpr(pattern, text, ignore.case=FALSE, perl=FALSE,
    	fixed=FALSE, useBytes=FALSE, extract=FALSE)

##  Arguments

pattern

character string containing a [ regular expression
](/link/regular%20expression?package=micropan&version=1.0) (or character
string for ` fixed = TRUE ` ) to be matched in the given character vector.
Coerced by ` [ as.character ](/link/as.character?package=micropan&version=1.0)
` to a character string if possible. If a character vector of length 2 or more
is supplied, the first element is used with a warning. Missing values are not
allowed.

text

a character vector where matches are sought, or an object which can be coerced
by ` [ as.character ](/link/as.character?package=micropan&version=1.0) ` to a
character vector.

ignore.case

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

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. See ` base::gregexpr ` for details.

extract

logical indicating if matching substrings should be extracted and returned.

##  Value

It will either return what the ` base::gregexpr ` would ( ` extract=FALSE ` )
or a  list  of substrings matching the pattern ( ` extract=TRUE ` ). There is
one  list  element for each string in  text  , and each list element contains
a character vector of all matching substrings in the corresponding entry of
text  .

##  Details

Extended version of ` [ gregexpr
](/link/gregexpr?package=micropan&version=1.0) ` that enables the return of
the substrings matching the pattern. The last argument  extract  is the only
difference to ` base::gregexpr ` . The default behaviour is identical to `
base::gregexpr ` , but setting ` extract=TRUE ` means the matching substrings
are returned.

##  See Also

` [ gregexpr ](/link/gregexpr?package=micropan&version=1.0&to=base) `

##  Examples

[ Run this code
](https://www.datacamp.com/datalab/preview?_tag=rdocs&rdocsPath=packages%2Fmicropan%2Fversions%2F1.0%2Ftopics%2Fgregexpr&utm_source=r-docs&utm_medium=docs&utm_term=gregexpr&utm_content=run_example_in_datalab)

    
    
      sequences<-c("ACATGTCATGTCC","CTTGTATGCTG")
      gregexpr("ATG",sequences,extract=TRUE)
    

Run the code above in your browser using  [ DataLab
](https://www.datacamp.com/datalab/preview?_tag=rdocs&rdocsPath=packages%2Fmicropan%2Fversions%2F1.0%2Ftopics%2Fgregexpr&utm_source=r-docs&utm_medium=docs&utm_term=gregexpr&utm_content=run_example_in_datalab)

Powered by  [ DataCamp ](https://www.datacamp.com/)