[英]Hash Map Key comparisons
I am making a hashmap by reading a particular section in the input file. 我正在通过读取输入文件中的特定部分来制作哈希图。 However the input files do not have a standard indentation format, ie, some of the lines start with a # characters , while some do not.
但是,输入文件没有标准的缩进格式,即,有些行以#字符开头,而有些则没有。 I am supposed to capture the key and value from the file.
我应该从文件中捕获键和值。 Here is my code:
这是我的代码:
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]);
}
}
Here is a section of input file : 这是输入文件的一部分:
#config ["Sql"] = \
#[
# ("KB",
# {
# "subsystem": "DMN",
# "enabled": "0",
# "freqtype": "4",
}
I just want the string including the double quotes in my key, ignoring the # character(which may or may not be present in a given file). 我只想在密钥中包含双引号的字符串,而忽略#字符(给定文件中可能存在也可能不存在)。 Is there any way?
有什么办法吗?
Search for matches of a regular expression like 搜索正则表达式的匹配项,例如
"[^"]*"
You can iterate over the elements in quotation marks by something like: 您可以通过类似以下操作来对引号中的元素进行迭代:
Pattern p = Pattern.compile("\"[^\"]*\"");
Matcher matcher = p.matcher(yourInputString);
while (matcher.find())
{
//do something with the match
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.