Main Content

matlab.unittest.constraints.Matches class

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

Constraint specifying string matches regular expression

Construction

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

expr

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 Pair Arguments

Specify optional comma-separated pairs of Name,Value arguments. Name is the argument name and Value is the corresponding value. Name must appear inside quotes. You can specify several name and value pair arguments in any order as Name1,Value1,...,NameN,ValueN.

'IgnoringCase'

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

Default: false

'WithCount'

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

Properties

Expression

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

IgnoreCase

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.

Examples

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:
        ^eps

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.