哈希映射键比较

时间:2016-07-07 11:50:54

标签: java hashmap

我通过读取输入文件中的特定部分来制作散列图。但是输入文件没有标准的缩进格式,即某些行以#字符开头,而有些则不是。我应该从文件中捕获密钥和值。 这是我的代码:

String schedule = "config [\"Sql\"] = \\";
        Scanner scanner = new Scanner(f1);
        while (scanner.hasNext()) {

            sCurrentLine = scanner.nextLine();

            if (sCurrentLine.contains(schedule)) {
                System.out.println(f1.getFileName()+" Contains a schedule");    
                scheduleFlag = true;
            }
            else if(sCurrentLine.contains("config [")) {
                scheduleFlag = false;
            }
            if(sCurrentLine.trim().startsWith("(")  && sCurrentLine.trim().endsWith(",") && scheduleFlag == true) {
                scheduleName = sCurrentLine;
                scheduleName = scheduleName.substring(scheduleName.indexOf('"')+1, scheduleName.lastIndexOf('"'));
                //writer.write(scheduleName);
                out.write(scheduleName.getBytes());
                HashMapping = true;

            }
            if(scheduleFlag == true && HashMapping == true) {
                String [] values = sCurrentLine.split(":");

                if (values.length < 2);
                else {
                hm.put(values[0], values[1]);
                }
            }

以下是输入文件的一部分:

#config ["Sql"] = \
#[
#   ("KB",
#       {
#           "subsystem":              "DMN",
#           "enabled":                "0",
#           "freqtype":               "4",
     }

我只想在我的密钥中包含双引号的字符串,忽略#字符(可能存在或不存在于给定文件中)。有什么办法吗?

1 个答案:

答案 0 :(得分:1)

搜索正则表达式的匹配项,例如

"[^"]*"

您可以通过以下内容迭代引号中的元素:

Pattern p = Pattern.compile("\"[^\"]*\"");
Matcher matcher = p.matcher(yourInputString);

while (matcher.find())
  {
      //do something with the match

  }