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 matches a regular expression. The constraint is satisfied only if the actual value matches the given regular expression, expr.

Matches(expr,'IgnoringCase',caseInsensitive) creates a constraint indicating whether to ignore case difference.

Input Arguments

expr

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

caseInsensitive

Indicator if the constraint is insensitive to case, specified as false or true (logical 0 or 1)

Default: false

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 input argument, ignoreCase. This property applies at all levels of recursion, such as nested structures.

Copy Semantics

Value. To learn how value classes affect copy operations, see Copying Objects in the MATLAB® documentation.

Examples

expand all

Test That Actual Value Matches Regular Expression

Create a test case for interactive testing.

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

testCase = TestCase.forInteractiveUse;

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

testCase.verifyThat('Epsilon Eridani', Matches('^eps'))
Interactive verification failed.

---------------------
Framework Diagnostic:
---------------------
Matches failed.
--> The string did not match the regular expression.

Actual String:
    Epsilon Eridani
Regular Expression:
    ^eps

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

testCase.verifyThat('Epsilon Eridani', Matches('^eps', ...
    'IgnoringCase', true))
Interactive verification passed.

Define the regular expression that the actual value must match.

expr = 'Some[Ss]?tring';

The [Ss]? contained in the regular expression indicates that either 'S' or 's' matches at that location 0 or 1 times.

Test that the actual values, 'SomeString' and 'Somestring', satisfy the constraint.

testCase.verifyThat('SomeString', Matches(expr))
testCase.verifyThat('Somestring', Matches(expr))
Interactive verification passed.
Interactive verification passed.

Test that the actual value 'Sometring' satisfies the constraint.

testCase.verifyThat('Sometring', Matches(expr))
Interactive verification passed.

Test that the actual value 'somestring' satisfies the constraint.

testCase.verifyThat('somestring', Matches(expr))
Interactive verification failed.

---------------------
Framework Diagnostic:
---------------------
Matches failed.
--> The string did not match the regular expression.

Actual String:
    somestring
Regular Expression:
    Some[Ss]?tring
Was this topic helpful?