Testing 正则表达式在结果中正好需要两个空格

Testing 正则表达式在结果中正好需要两个空格,testing,automation,automated-tests,soapui,Testing,Automation,Automated Tests,Soapui,正则表达式,以在结果中仅允许两个空格。示例:“一百美元”仅包含两个空格,如果有两个以上的空格,则条件应失败 示例:230445 包含2个以上的空格,因此条件应失败使用\s检测空白 例如,接受“我是学生”或“一百美元”,你可以有 /^[A-Za-z]+\s[A-Za-z]+\s[A-Za-z]+$/ 虽然我不确定它是否有效。。我没有检查。这里是groovy脚本,它执行您正在寻找的断言 //String sample to test both negative and positive tests

正则表达式,以在结果中仅允许两个空格。示例:“一百美元”仅包含两个空格,如果有两个以上的空格,则条件应失败

示例:230445


包含2个以上的空格,因此条件应失败

使用\s检测空白

例如,接受“我是学生”或“一百美元”,你可以有

/^[A-Za-z]+\s[A-Za-z]+\s[A-Za-z]+$/


虽然我不确定它是否有效。。我没有检查。

这里是groovy脚本,它执行您正在寻找的断言

//String sample to test both negative and positive tests

//This string should fail the test
​def string1 = 'two three thousand four hundred fifty five'

//this string should pass validation
def string2 = 'two three thousand'

//Expected space count
def expectedSpaces = 2
//Check for number of occurances of spaces in the given string
def group1 = (string1 =~​ /\s/)
def group2 = (string2 =~ /\s/ )

println "${string2} has ${group2.size()} spaces"
println "${string1} has ${group1.size()} spaces"

//Positive assertion
assert expectedSpaces == group2.size(), "${string2} failed assertion"
//You may find this failing as expected, so negative assertion
assert expectedSpaces != group1.size(), "${string1} failed assertion"​

您可以快速检查此项。

下面是接受包含3个单词和2个空格的字符串的正则表达式

/^([A-Za-z])+\s([A-Za-z])+\s([A-Za-z]+)$/
解释

  • ^:字符串的开头
  • ([A-Za-z]+):接受带有一个或多个字符的单词 性格
  • \s:空白
  • $:字符串的结尾

您试图应用哪种断言?是
脚本断言
还是
Xpath
?您可以查看答案并接受适合您的答案。ContainsHow中的Xpath您的原始响应看起来像什么?