[英]Java - How to validate this string?
它是否存在Java中用于执行以下任务的工具?
我得到了这个难输入的字符串:{[1; 3] || [7; 9; 10-13]}
大括号{}表示必填
方括号[]表示必需的组
双管|| 表示“或”
阅读上面的字符串,我们得到以下信息:
如果为true,它将通过。 如果为假,则不会通过。
我正在尝试使用硬编码进行此操作,但是我很沮丧地发现这种类型的验证比较容易或正确。
我必须学习哪种类型的内容以了解更多信息?
我从这段代码开始,但是我认为这是不对的:
//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
}
}
因此,规则对我而言并不十分清楚。 例如,您仅关注||
吗? 或者您还有&&
吗? 如果看您的示例,我可以从中得出&&和运算符在;
是隐式的;
。
尽管如此,我还是编写了一个代码示例(没有太多正则表达式)来检查您的规则。
首先,您需要从||
开始。 操作员。 将所有不同的OR语句放入String
块。
接下来,您将需要检查String
块中的每个元素,并检查输入值是否包含所有块值。
如果是这样,那么输入字符串包含您设置的所有规则必须为真。
如果您的规则包含一个范围,则必须首先完全填充范围块,然后再对范围块执行相同的操作,就像对普通规则值所做的一样。
下面的完整代码示例。
package nl.stackoverflow.www.so;
import java.util.ArrayList;
import java.util.List;
public class App
{
private String rules = "{[1;3] || [7;9;10-13] || [34;32]}";
public static void main( String[] args )
{
new App();
}
public App() {
String[] values = {"11 12", "10 11 12 13", "1 2 3", "1 3", "32 23", "23 32 53 34"};
// Iterate over each value in String array
for (String value : values) {
if (isWithinRules(value)) {
System.out.println("Success: " + value);
}
}
}
private boolean isWithinRules(String inputValue) {
boolean result = false;
// || is a special char, so you need to escape it with \. and since \ is also a special char
// You need to escape the \ with another \ so \\| is valid for one | (pipe)
String[] orRules = rules.split("\\|\\|");
// Iterate over each or rules
for (String orRule : orRules) {
// Remove [] and {} from rules
orRule = orRule.replace("[", "");
orRule = orRule.replace("]", "");
orRule = orRule.replace("{", "");
orRule = orRule.replace("}", "");
orRule.trim();
// Split all and rules of or rule
String[] andRules = orRule.split(";");
boolean andRulesApply = true;
// Iterate over all and rules
for (String andRule : andRules) {
andRule = andRule.trim();
// check if andRule is range
if (andRule.contains("-")) {
String[] andRulesRange = andRule.split("-");
int beginRangeAndRule = Integer.parseInt(andRulesRange[0]);
int endRangeAndRule = Integer.parseInt(andRulesRange[1]);
List<String> andRangeRules = new ArrayList<String>();
// Add all values to another rule array
while (beginRangeAndRule < endRangeAndRule) {
andRangeRules.add(Integer.toString(beginRangeAndRule));
beginRangeAndRule++;
}
for (String andRangeRule : andRangeRules) {
// Check if andRule does not contain in String inputValue
if (!valueContainsRule(inputValue, andRangeRule)) {
andRulesApply = false;
break;
}
}
} else {
// Check if andRule does not contain in String inputValue
if (!valueContainsRule(inputValue, andRule)) {
andRulesApply = false;
break;
}
}
}
// If andRules apply, break and set bool to true because string contains all andRules
if (andRulesApply) {
result = true;
break;
}
}
return result;
}
private boolean valueContainsRule(String val, String rule) {
boolean result = true;
// Check if andRule does not contain in String inputValue
if (!val.contains(rule)) {
result = false;
}
return result;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.