消耗Java字符串数组来设置键值对

时间:2018-11-07 20:23:13

标签: java arrays

在这里难以记住状态。感谢任何帮助。我有一个由最终用户设置的字符串,没有验证,是开放文本。我需要接受它并设置键值对。我忽略了所有其他行(例如:以:或!开头)我确实知道键将以短划线开头(例如:-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

1 个答案:

答案 0 :(得分:0)

我建议使用Regex来执行此操作。 您可以像这样捕获第一个示例:

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/ 

如果进一步扩展正则表达式,您将能够完全按照您的描述进行捕获。