Main Content

matlab.unittest.constraints.Matches 类

命名空间: matlab.unittest.constraints
超类: matlab.unittest.constraints.BooleanConstraint

测试值是否与指定的正则表达式匹配

描述

matlab.unittest.constraints.Matches 类提供一个约束来测试值是否与指定的正则表达式匹配。

创建对象

描述

示例

c = matlab.unittest.constraints.Matches(expression) 创建一个约束来测试值是否与指定的正则表达式匹配。与 expression 匹配的字符串标量或字符向量满足该约束。

示例

c = matlab.unittest.constraints.Matches(expression,Name,Value) 使用一个或多个名称-值参量设置其他选项。例如,c = matlab.unittest.constraints.Matches(expression,"IgnoringCase",true) 创建一个不区分大小写的约束。

输入参量

全部展开

要匹配的正则表达式,指定为非空字符串标量或字符向量。

此参量设置 Expression 属性。

名称-值参数

将可选的参量对组指定为 Name1=Value1,...,NameN=ValueN,其中 Name 是参量名称,Value 是对应的值。名称-值参量必须出现在其他参量之后,但参量对组的顺序无关紧要。

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

在 R2021a 之前,使用逗号分隔每个名称和值,并用引号将 Name 引起来。

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

是否忽略大小写,指定为数值或逻辑值 0 (false) 或 1 (true)。默认情况下,约束区分大小写。

此参量设置 IgnoreCase 属性。

匹配 expression 的次数,指定为正整数标量。

您可以指定此名称-值参量,以便只对不重叠的匹配项计数。例如,下面的试失败。

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

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

数据类型: double | single | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64

属性

全部展开

要匹配的正则表达式,以字符串标量或字符向量形式返回。

此属性由 expression 输入参量设置。

属性:

GetAccess
public
SetAccess
immutable

是否忽略大小写,以逻辑值 0 (false) 或 1 (true) 形式返回。默认情况下,约束区分大小写。

此属性由 IgnoringCase 名称-值参量设置。

属性:

GetAccess
public
SetAccess
private

示例

全部折叠

使用 Matches 约束测试字符串是否与正则表达式匹配。

首先,导入此示例中使用的类。

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

创建一个供交互测试的测试用例。

testCase = TestCase.forInteractiveUse;

验证字符串 "Some Text" 与正则表达式 "^Som" 匹配。

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

展示大小写不同所造成的影响。以下测试失败,因为 actual 值不以小写字母开头。

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"

要使测试通过,请忽略大小写。

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

现在,指定一个不同的正则表达式。表达式中的值 [Tt]? 指示 "T""t" 在该位置匹配 0 次或 1 次。

expression = "Some[Tt]?ext";

验证字符串 "SomeText"expression 匹配。

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

测试 "SomeText Sometext Someext" 是否与 expression 匹配三次。测试通过。

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

验证字符串 "sometext" 与正则表达式不匹配。

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

版本历史记录

在 R2013a 中推出

全部展开