簡體   English   中英

Java-如何驗證此字符串?

[英]Java - How to validate this string?

它是否存在Java中用於執行以下任務的工具?

我得到了這個難輸入的字符串:{[1; 3] || [7; 9; 10-13]}

  • 大括號{}表示必填

  • 方括號[]表示必需的組

  • 雙管|| 表示“或”

閱讀上面的字符串,我們得到以下信息:

  • 要求 某些STRING具有1和3 7和9和10、11、12和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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM