正则表达式,结果只允许两个空格。例如:"一百美元"只包含两个空格,如果有超过2个空格,则条件应该失败
例子:两个三千四百五十五个
包含2个以上的空格,因此条件应该失败
答案 0 :(得分:3)
下面是正则表达式,它接受3个字和2个空格的字符串
/^([A-Za-z])+\s([A-Za-z])+\s([A-Za-z]+)$/
解释
答案 1 :(得分:0)
使用 \ s 来检测空白区域。
例如,接受“我是学生”或“一百美元”你可以拥有../ ^ [A-Za-z] + \ s [A-Za-z] + \ s [A-Za-z] + $ /
虽然我不确定它是否有效..我没有检查。
答案 2 :(得分:0)
这是一个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"
您可以快速查看script。