Java如何验证这个字符串?
Java中是否存在执行以下此类任务的工具
我得到了这个硬输入字符串:{[1;3]|[7;9;10-13]}
{p}表示括号是必需的
方括号[]表示需要的组
双管的意思是“或”
阅读上面的字符串,我们得到以下结果:
- 要求某些字符串具有1和3或<7和9以及10、11、12和13
如果是真的,它将通过。如果为false,则不会通过
我试图用硬编码来实现这一点,但我觉得有一种更简单或正确的方法来实现这种类型的验证
我必须学习哪种类型的内容才能了解更多信息
我从这段代码开始,但我觉得这不对:
//Gets the string
String requiredGroups = "{[1;3]||[7;9;10-13]}";
//Gets the groups that an Object belongs to
//It will return something like 5,7,9,10,11,12
List<Integer> groupsThatAnObjectIs = object.getListOfGroups();
//Validate if the Object is in the required groups
if ( DoTheObjectIsInRequiredGroups( groupsThatAnObjectIs, requiredGroups ) ) {
//Do something
}
我试图使用这个迭代器从requiredGroups变量中获取所需的值
//Used for values like {[1;3]||[9;10;11-15]} and returns the required values
public static void IterateRequiredValues(String values, List<String> requiredItems) {
values = values.trim();
if( !values.equals("") && values.length() > 0 ) {
values = values.replace("{", "");
values = values.replace("}", "");
String arrayRequiredItems[];
if ( values.contains("||") ) {
arrayRequiredItems = values.split("||");
}
//NOTE: it's not done yet
}
}
# 1 楼答案
所以规则对我来说不是很清楚。 例如,您是只关注
||
还是同时关注&&
? 如果我看看你的例子,我可以从中得出&;and运算符隐含在;
中尽管如此,我还是制作了一个代码示例(没有太多正则表达式)来检查您的规则
首先需要从
||
操作符开始。 将所有不同的OR语句放入String
块中接下来,您需要检查
String
块中的每个元素,并检查输入值是否包含所有块值如果是这样,那么您的输入字符串必须包含您设置的所有规则
如果规则包含一个范围,则必须首先完全填充范围块 然后对范围块执行与普通规则值相同的操作
完成下面的代码示例