MATLAB Function Reference    

Match regular expression



start = regexp(str,expr) returns a row vector, start, containing the indices of the substrings in str that match the regular expression string, expr.

When either str or expr is a cell array of strings, regexp returns an m-by-n cell array of row vectors of indices, where m is the the number of strings in str and n is the number of regular expression patterns in expr.

[start,finish] = regexp(str,expr) returns an additional row vector finish, that contains the indices of the last character of the corresponding substrings in start.

[start,finish,tokens] = regexp(str,expr) returns a 1-by-n cell array, tokens, of beginining and ending indices of tokens within the corresponding substrings in start and finish. Tokens are denoted by parentheses in the expression, expr.

[...] = regexp(str,expr,'once') finds just the first match. (By default, regexp returns all matches.) If no matches are found, then all return values are empty.


See Regular Expressions, in the MATLAB documentation, for a listing of all regular expression metacharacters supported by MATLAB.

regexp does not support international character sets.


Example 1

Return a row vector of indices that match words that start with c, end with t, and contain one or more vowels between them:

Example 2

Return a cell array of row vectors of indices that match capital letters and whitespaces in the cell array of strings, str:

Capital letters, '[A-Z]', were found at these str indices:

Space characters, '\s', were found at these str indices:

Example 3

Return the starting and ending indices of words containing the letter x:

Example 4

Return the starting and ending indices of substrings contained by the letter s. Also return the starting and ending indices of the token defined within the parentheses:

See Also

regexpi, regexprep, strfind, findstr, strmatch, strcmp, strcmpi, strncmp, strncmpi

  refresh regexpi