Search for Pattern in Text

DESCRIPTION:
Searches for a text pattern as described by a regular expression in a vector of character strings.

USAGE:
grep(pattern, text)

REQUIRED ARGUMENTS:
pattern:
character string giving a regular expression. Regular expressions are built up as for the egrep UNIX command. that may include wildcards. The following wildcards are available:

* zero or more occurrences of the previous character. .* matches any sequence of zero or more characters, irrespective of the previous character. ? matches any single character. [ ] encloses a set of values and matches any character within the set. Any continuous subset of ([A-Z]) is allowed. ! used within [ ] matches any character except those specified in the set.

text:
vector of character strings.

VALUE:
numeric vector telling which elements of text matched pattern. It returns numeric(0) when there are no matches. In all cases, the return value can be used as a subscript to retrieve the matching elements of text.

DETAILS:
Since the backslash () is a special character in S-PLUS, if a backslash is used with grep in an S-PLUS expression it must be specified as a double backslash (\), one for the S-PLUS interpreter and one for grep. See the EXAMPLES section below for more information.

SEE ALSO:
charmatch , match .

EXAMPLES:
state.name[ grep("ia$", state.name) ]
  # find state names that end in "ia"
state.name[ grep("ia$|^M", state.name) ]
  # find state names that end in "ia" or start with "M"
  # Note matching is not the same as in ls
state.name [grep("Ne", state.name)]
  # returns all states beginning with "Ne"
state.name [grep ("Neb", state.name)]
  # returns Nebraska
state.name [grep("Ne*", state.name)]
  # returns all states beginning with "N"
state.name [grep ("Ne.*", state.name)]
  # returns all states beginning with "Ne"
state.name [grep ("Ne[a-e]", state.name)]
  # returns Nebraska
# using a backslash with grep in S-PLUS:
str <- c("SP500","S.P500")
grep("^S.", str)
  [1] 1 2
grep("^S.", str) # same as above because S removes the   [1] 1 2
grep("^S\.", str) # what we want
[1] 2