Name

grep — find matches of a string in a vector of strings

Calling Sequence

row=grep(haystack,needle )
[row,which]=grep(haystack,needle )
row=grep(haystack,needle ,[flag])
[row,which]=grep(haystack,needle ,[flag])

Parameters

haystack

A Row vector of character strings.

needle

A character string or a row vector of character strings . The string(s) to search in haystack.

row

vector of indices: row where a match has been found or an empty matrix if no match found.

which

vector of indices: index of needle string found or an empty matrix if no match found.

flag

Character ("r" for regular expression)

Description

Foreach entry of haystack , grep searches if at least a string in needle matches a substring. haystack entries index where at least a match has been found are returned in the row argument. while optionnal which argument gives the index of first string of needle found. When using the third parameters "r", the needle should be a string of regular expression. And then grep is going to match it with haystack according to the regular express rules.

Examples


txt=['find matches of a string in a vector of strings'
     'search position of a character string in an other string'
     'Compare Strings'];

grep(txt,'strings')
grep(txt,['strings' 'Strings'])

[r,w]=grep(txt,['strings' 'Strings'])

str = ["hat";"cat";"hhat";"chat";"hcat";"ccchat";"at";"dog"]

grep(str,'/[hc]+at/','r')
grep(str,'/[hc]?at/','r')
grep(str,'/cat|dog/','r')

  

See Also

strindex