在这里我想为仅包含0到6个数字的字符串制作regx。
该字符串包含0到6这样的数字。
Example-1 : "010002030405" Valid String
此字符串仅包含o到6个数字,因此在这里我使用了此regx
"[0-6]*"
。但是我想在此字符串中验证的另一件事是,我只希望0在奇数位置1-6永远不会在奇数位置。 0可以放在奇数和偶数位置,但1-6只能放在偶数位置。在这里我给你一些有效和无效的字符串示例
Valid : 000102000004
invalid : 0023015006
在这里我使用了此代码,请建议我或告诉我我必须在regx中进行哪些更改才能满足以下验证要求
1) String contains only 0-6 numbers nothing else.
2) 1-6 would be only even positions only they would not be at odd position ever, 0 would be odd and even position.
代码:
public boolean isOptions(String input) {
String patternString = "[0-6]*";
Pattern pattern = Pattern.compile(patternString);
return pattern.matcher(input).matches();
}
最佳答案
尚未尝试过,但可能有效:
(0[0-6])*0?