difference between strfind() and regexp() in matlab

strfind() is a function that finds the starting index of the first occurrence of a substring within a larger string. For example:

main.m
str = 'Hello World';
idx = strfind(str, 'lo'); % returns 4
59 chars
3 lines

regexp() is a function that finds one or multiple matches of a regular expression within a larger string. For example:

main.m
str = 'John has 5 apples, Jane has 10 oranges, and Bob has 3 pears';
matches = regexp(str, '\d+', 'match'); % returns {'5', '10', '3'}
135 chars
3 lines

The first argument to regexp() is the string to search within. The second argument is the regular expression to match. The third optional argument is a flag to indicate if the function should return all the matches or just the first one (default).

In short, strfind() is used to find a substring within a larger string, while regexp() is used to find one or more matches of a regular expression within a larger string.

related categories

gistlibby LogSnag