簡體   English   中英

消費Java字符串數組以設置鍵值對

[英]Consuming Java String Array to set Key Value Pairs

無法在此處牢記狀態。 感謝任何幫助。 我有一個由最終用戶設置的字符串,沒有驗證,是開放文本。 我需要接受它並設置鍵值對。 我忽略了所有其他行(例如:以:或!開頭)我確實知道鍵將以短划線開頭(例如:-filePickupDir),然后該值將在下一個空格之后或在下一個空格后的換行符。 如果存在連續斜杠(),那么我知道還有另一個鍵值對。 有關用戶如何將其放置到位的一些示例:

-filePickupDir / export / home / PickupDir / \\

要么

-filePickupDir \\

/ export / home / AdjPickupDir / \\

示例代碼:

HashMap<String, String> processMap = new HashMap<>();
    String jobProcess = job.getProcess(); //this is a method that gets the string
    String lines[] = jobProcess.split("[\\r\\n]+");

    int varCount = 0;
    for (String s: lines) {
        String key = "";
        String val = "";
        int count = 0;
        int count2 = 0;
        if (s.startsWith("!")) {

        } else if (s.startsWith(":")) {

        } else if (s.startsWith("-")) {
            count = s.length() - s.replace(" ", "").length();
            count2 = s.length() - s.replace("\\", "").length();
            System.out.println("Line space count: " + count + " continue line count: " + count2);
            if (count == 1 && count2 == 0 || count == 3 && count2 == 1 || count == 1 && count2 == 1) {
                s = s.trim();
                int keyIndex = s.indexOf("-");
                keyIndex = +1;
                int firstSpaceIndex = s.indexOf(" ");
                int spaceAfterFirstSpaceIndex = firstSpaceIndex + 1;
                int lastIndex = s.length();
                String keyString = s.substring(keyIndex, firstSpaceIndex);
                String valueString = s.substring(spaceAfterFirstSpaceIndex, lastIndex);
                if (count == 3 && count2 == 1) {
                    int removeSlashIndex = valueString.length();
                    valueString = valueString.substring(1, removeSlashIndex - 3);
                }
            } else if (count == 1 && count2 == 1) {
                //value is on the next line
                //We need to let the program know we have a key but no value and need to maintain state
            }

            //String split = String.valueOf(s.split("^-(\\w|\\d|\\s)+"));
            //System.out.println("split is: "+split.toString());


        } else {
            //This is value if the key is on its own line above
            s = s.trim();
            System.out.println("Value: " + s);
        }
    }

我在將自己的頭保持在此處狀態時遇到了問題。 我基本上只需要一個以斜杠(-)開頭的鍵,然后該值是下一個由空格或空格和換行符分隔的字符串。 繼續處理,直到找到所有密鑰對。

要使用的示例字符串數組:

-hostIds \\

9 \\

-maxRecords \\

1000 \\

-xsl \\

$ batchslx / ziproot / EmailXsl

我建議使用正則表達式來做到這一點。 您可以捕獲第一個示例,如下所示:

String firstTest = "-filePickupDir /export/home/PickupDir/ \\";

//the capture groups are in the parantheses
String patternString = "[\\n\\s]?-(\\w+)(.+)\\\\";

Pattern pat =  Pattern.compile(patternString);
Matcher match = pat.matcher(firstTest);
match.find();

System.out.println(match.group(0)); //whole String
System.out.println(match.group(1)); //first capture group
System.out.println(match.group(2)); //second capture group
//results:
//0 -filePickupDir /export/home/PickupDir/ \
//1 filePickupDir
//2 /export/home/PickupDir/ 

如果您進一步擴展正則表達式,您將能夠完全按照您的描述進行捕獲。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

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