我有以下深层链接,如果字符串有 2 个或更多问号,并且理想情况下应始终包含一个问号,则正则表达式应该失败
这是我正在使用的正则表达式:"^(?!.*[?][?].*)+$"
TestMe?src=SRCH&reset=true - regex should pass
TestMe?src=SRCH?reset=true - regex should fail
最佳答案
您正在寻找 2 个相邻的 ?:
"^(?!.[?][?].)+$"
^ ^
你的“失败”字符串没有:
TestMe?src=SRCH?reset=true
^--------^
您不需要使用正则表达式来检查两个问号:
boolean containsTwoQuestionMarks(String str) {
return str.indexOf('?') != str.lastIndexOf('?');
}
关于java - 正则表达式 - 链接不允许双问号 Java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40877765/