Main Content

matlab.unittest.constraints.Matches class

Package: matlab.unittest.constraints
Superclasses: matlab.unittest.constraints.BooleanConstraint

Constraint specifying string matches regular expression


Matches(expr) creates a constraint that specifies that a string scalar or character vector matches a regular expression. The constraint is satisfied only if the actual value matches the given regular expression, expr.

Matches(expr,Name,Value) provides a constraint with additional options specified by one or more Name,Value pair arguments. Name must appear inside single quotes (''). You can specify several name-value pair arguments in any order as Name1,Value1,...,NameN,ValueN.

Input Arguments


Regular expression that the actual value must match to satisfy the constraint, specified as a string scalar or character vector. expr can include newline characters.

Name-Value Arguments

Specify optional pairs of arguments as Name1=Value1,...,NameN=ValueN, where Name is the argument name and Value is the corresponding value. Name-value arguments must appear after other arguments, but the order of the pairs does not matter.

Before R2021a, use commas to separate each name and value, and enclose Name in quotes.


Indicator to ignore case, specified as false or true (logical 0 or 1).

Default: false


Number of times the actual value must match expr, specified as a positive integer.



Regular expression that the actual value must match, specified in the input argument, expr.


Indicator if the constraint is insensitive to case, specified in the name-value pair argument, 'IgnoringCase'.

Copy Semantics

Value. To learn how value classes affect copy operations, see Copying Objects.


collapse all

Create a test case for interactive testing.

import matlab.unittest.TestCase
import matlab.unittest.constraints.Matches

testCase = TestCase.forInteractiveUse;

Test that the actual value 'Epsilon Eridani' matches 'eps'.

testCase.verifyThat('Epsilon Eridani', Matches('^eps'));
Verification failed.
    Framework Diagnostic:
    Matches failed.
    --> The value does not match the regular expression.
    Actual char:
        Epsilon Eridani
    Regular Expression:

To satisfy the constraint, configure it to be case insensitive.

testCase.verifyThat('Epsilon Eridani', Matches('^eps', ...
    'IgnoringCase', true));
Verification passed.

Define the regular expression that the actual value must match.

expr = 'Some[Tt]?ext';

The [Tt]? contained in the regular expression indicates that either 'T' or 't' matches at that location 0 or 1 times.

Test that the actual values, 'SomeText' and 'Sometext', satisfy the constraint.

testCase.verifyThat('SomeText', Matches(expr));
testCase.verifyThat('Sometext', Matches(expr));
Verification passed.
Verification passed.

Test that the actual value 'SomeText Sometext Someext' matches the regular expression three times.

testCase.verifyThat('SomeText Sometext Someext', Matches(expr, ...
    'WithCount', 3));
Verification passed.

Test that the actual value 'sometext' does not satisfy the constraint.

testCase.verifyThat('sometext', ~Matches(expr));
Verification passed.