grepfind matches of a string in a vector of stringsCalling Sequencerow = grep(haystack, needle )
[row, which] = grep(haystack, needle )
row = grep(haystack, needle, [flag])
[row, which] = grep(haystack, needle, [flag])
Argumentshaystacka row vector of character strings.needle
a character string or a row vector of character strings. The
string(s) to search in haystack.
row
a vector of indices: row where a match has been found or an
empty matrix if no match found.
which
a vector of indices: index of needle string found
or an empty matrix if no match found.
flag
a character ("r" for regular expression).
Description
For each entry of haystack, grep
searches if at least a string in needle which matches a
substring. The haystack entries index where at least a
match has been found are returned in the row output
argument. The optional which output argument gives the
index of first string of needle found.
When using the third parameter flag="r", the
needle is expected to be a regular expression string.
In this case, grep uses the
needle as a regular expression and compares it
against haystack according to the regular expressions
rules. See the regexp function for details about
regular expressions.
Example #1
In the following example, we search one or two strings in a text,
which is stored in the txt variable.
Example #2In the following example, we perform regexp searches.See Also
strindex
regexp