regexp

Returns the results after searching for the regular expression or pattern, pattern, in the input, s.

Syntax

[startidx, endidx, extent, match, token, namedtoken, unmatchedtoken] = regexp(s, pattern)

[startidx, ...] = regexp(s, pattern, options)

Inputs

s
Type: string | mat
pattern
The pattern to match.
Type: string
options (optional)
Argument(s) specifying the search pattern. Currently supported options include:
ignorecase
Ignores the case when matching the pattern.
matchcase
Matches the case when matching the pattern.
emptymatch
Includes zero length matches.
Type: string

Outputs

startidx
Start index of each substring matching the pattern.
Type: integer
endidx
End index of each substring matching the pattern.
Type: integer
extent
The extents or ranges of matched tokens.
Type: cell
match
Cell array of matched substrings.
token
Cell array of matched tokens.
Type: cell
namedtoken
Cell array of matched, named tokens.
Type: cell
unmatchedtoken
Cell array of unmatched tokens.
Type: cell

Examples:

Regular expression matching all outputs:
[startidx, endidx, extent, match, token, namedtoken, unmatchedtoken] = regexp('test',['test';'test'])
startidx = 1
endidx = 4
extent =
{
  [1,1] [Matrix] 0 x 2
}
match =
{
  [1,1] test
}
token =
{
  [1,1]
{
}
}
namedtoken = struct [ ]
unmatchedtoken =
{
  [1,1]
  [1,2]
}
Regular expression matching with case ignored:
R = regexp({'FILE021.prn', 'FILE0.prn'}, '.PRN','ignorecase')
R =
{
  [1,1] 8
  [1,2] 6
}
Regular expression matching with case considered:
R = regexp({'FILE021.prn', 'FILE0.PRN'}, '.PRN','matchcase')
R =
{
  [1,1] [Matrix] 1 x 0
  [1,2] 6
}
Regular expression matching with empty matches:
[~, ~, ~, match, ~, ~, ~]=regexp('aaa.bbb.ccc.ddd','[^\.]*','emptymatch')
R =
match = 
{
  [1,1] aaa
  [1,2] 
  [1,3] bbb
  [1,4] 
  [1,5] ccc
  [1,6] 
  [1,7] ddd
  [1,8] 
}
Regular expression matching with any character except line terminators:
str = sprintf('\nabc');
pattern = '.*';
[~, ~, ~, match, ~, ~, ~]= regexp(str,pattern)
match = 
{
  [1,1] abc
}

Comments

Special pattern character "." matches any character except line terminators.