有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

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) 个答案

  1. # 1 楼答案

    所以规则对我来说不是很清楚。 例如,您是只关注||还是同时关注&&? 如果我看看你的例子,我可以从中得出&;and运算符隐含在;

    尽管如此,我还是制作了一个代码示例(没有太多正则表达式)来检查您的规则

    首先需要从||操作符开始。 将所有不同的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;
        }
    }