grep(pattern, text)
* 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.
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