Should Match Regexp是RF BuildIn关键字,入参为string, pattern,msg=None,values=True;前2个参数为必选,后2个参数为可选参数。基于Python re模块实现。@H_403_21@
说明
(1)\是转义字符@H_403_21@
(2)匹配字符串可包含特殊字符,但在字符串中午特殊含义,只是文本意思。@H_403_21@
(3)匹配模式不是全字符匹配。若需要全字符匹配,在匹配模式字符首加^,字符尾加$@H_403_21@
@H_403_21@
${ret}= | Should Match Regexp | 123abc123 | abc |
log | ${ret} |
执行结果@H_403_21@
0170213 06:08:15.031 : INFO : ${ret} = abc
20170213 06:08:15.031 : INFO : abc@H_403_21@
@H_403_21@
${ret}= | Should Match Regexp | 123abc123 | ^abc$ |
log | ${ret} |
执行结果@H_403_21@
20170213 06:13:05.890 : FAIL : '123abc123' does not match '^abc$'@H_403_21@
@H_403_21@
${ret}= | Should Match Regexp | abc | ^abc$ |
log | ${ret} |
执行结果@H_403_21@
20170213 06:15:33.515 : INFO : ${ret} = abc
20170213 06:15:33.515 : INFO : abc@H_403_21@
(4)可选标识@H_403_21@
i 不区分大小写@H_403_21@
u依赖于Unicode@H_403_21@
L本地化@H_403_21@
@H_403_21@
返回值
成功
返回匹配成功的字符串,也可以返回匹配的子字符串。@H_403_21@
实例如下:匹配字符串str1,值为1 MSV 3 V1.1 inservice;匹配模式为1\\s+(\\w+)\\s+(\\d+)\\s+(\\S+)\\s+(\\S+);@H_403_21@
匹配成功的字符串赋给变量match,匹配成功的子字符串赋给变量cardtype、portnum、ver、status@H_403_21@
执行结果,match、cardtype、portnum、ver、status的值如下:@H_403_21@
失败
msg默认为None,可以给其赋值,当执行失败后会打印该提示。@H_403_21@
@H_403_21@
${output1} | set variable | FO1o:11 | ||
${ret}= | Should Match Regexp | ${output1} | (?i)foo:\\d+ | msg=匹配失败 |
执行结果 Starting test: Testproject2.Testsuite.ShouldMatchRegexp 20170213 06:35:32.296 : INFO : ${output} = 123456ab 20170213 06:35:32.312 : INFO : ${output1} = FO1o:11 20170213 06:35:32.312 : INFO : ${outmatch} = 123456 20170213 06:35:32.312 : FAIL : 匹配失败: 'FO1o:11' does not match '(?i)foo:\d+' Ending test: Testproject2.Testsuite.ShouldMatchRegexp@H_403_21@