Main Content

matlab.unittest.constraints.Matches class

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

Test if value matches specified regular expression

Description

The matlab.unittest.constraints.Matches class provides a constraint to test if a value matches a specified regular expression.

Creation

Description

example

c = matlab.unittest.constraints.Matches(expression) creates a constraint to test if a value matches the specified regular expression. The constraint is satisfied by a string scalar or character vector that matches expression.

example

c = matlab.unittest.constraints.Matches(expression,Name,Value) sets additional options using one or more name-value arguments. For example, c = matlab.unittest.constraints.Matches(expression,"IgnoringCase",true) creates a constraint that is insensitive to case.

Input Arguments

expand all

Regular expression to match, specified as a nonempty string scalar or character vector.

This argument sets the Expression property.

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.

Example: c = matlab.unittest.constraints.Matches(expression,IgnoringCase=true)

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

Example: c = matlab.unittest.constraints.Matches(expression,"IgnoringCase",true)

Whether to ignore case, specified as a numeric or logical 0 (false) or 1 (true). By default, the constraint is sensitive to case.

This argument sets the IgnoreCase property.

Number of times to match expression, specified as a positive integer scalar.

You can specify this name-value argument to count only nonoverlapping occurrences of a match. For example, this tests fails.

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

testCase = TestCase.forInteractiveUse;
testCase.verifyThat("eyeye",Matches("e.e","WithCount",2))

Data Types: double | single | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64

Properties

expand all

Regular expression to match, returned as a string scalar or character vector.

This property is set by the expression input argument.

Attributes:

GetAccess
public
SetAccess
immutable

Whether to ignore case, returned as a logical 0 (false) or 1 (true). By default, the constraint is sensitive to case.

This property is set by the IgnoringCase name-value argument.

Attributes:

GetAccess
public
SetAccess
private

Examples

collapse all

Test if a string matches a regular expression by using the Matches constraint.

First, import the classes used in this example.

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

Create a test case for interactive testing.

testCase = TestCase.forInteractiveUse;

Verify that the string "Some Text" matches the regular expression "^Som".

testCase.verifyThat("Some Text",Matches("^Som"))
Verification passed.

Show that case matters. This test fails because the actual value does not start with a lowercase letter.

testCase.verifyThat("Some Text",Matches("^som"))
Verification failed.
    ---------------------
    Framework Diagnostic:
    ---------------------
    Matches failed.
    --> The value does not match the regular expression.
    
    Actual Value:
        "Some Text"
    Regular Expression:
        "^som"

For the test to pass, ignore case.

testCase.verifyThat("Some Text",Matches("^som","IgnoringCase",true))
Verification passed.

Now, specify a different regular expression. The value [Tt]? in the expression indicates that either "T" or "t" matches at that location 0 times or 1 time.

expression = "Some[Tt]?ext";

Verify that the string "SomeText" matches expression.

testCase.verifyThat("SomeText",Matches(expression))
Verification passed.

Test if "SomeText Sometext Someext" matches expression three times. The test passes.

testCase.verifyThat("SomeText Sometext Someext",Matches(expression, ...
    "WithCount",3))
Verification passed.

Verify that the string "sometext" does not match the regular expression.

testCase.verifyThat("sometext",~Matches(expression))
Verification passed.

Version History

Introduced in R2013a

expand all