[英]Parse a multiline string in JAVA that includes spaces in between
I am trying to parse the following block of string that has four spaces before and between keys, datatype and values: 我正在尝试解析以下字符串块,该字符串块在键,数据类型和值之前和之间有四个空格:
PYTHON_HOME REG_SZ C:\Python27;C:\Python27\Scripts
PYTHON_PATH REG_SZ C:\tsde\Python\v34
SCALA_HOME REG_SZ C:\Program Files (x86)\scala
SZ REG_SZ C:\Program Files\7-Zip
TEMP REG_EXPAND_SZ %USERPROFILE%\AppData\Local\Temp
TMP REG_EXPAND_SZ %USERPROFILE%\AppData\Local\Temp
I would like to parse the string and assign parsed values to variables or a multi-dimensional array. 我想解析字符串并将解析后的值分配给变量或多维数组。 The expected result that I am looking for is something like: 我正在寻找的预期结果是这样的:
Key1 = PYTHON_HOME, Value1 = C:\Python27;C:\Python27\Scripts
Key2 = SCALA_HOME, Value2 = C:\Program Files (x86)\scala
Key3 = SZ, Value3 = C:\Program Files\7-Zip
Key4 =TEMP, Value4 = %USERPROFILE%\AppData\Local\Temp
Key5 = TMP, Value5 = %USERPROFILE%\AppData\Local\Temp
So far I have been playing with pattern and matcher in java.util.regex and haven't actually gotten anywhere. 到目前为止,我一直在使用java.util.regex中的模式和匹配器,但实际上并没有得到任何帮助。
Please note that the given block of string may have more lines of keys, dataType and values. 请注意,给定的字符串块可能包含更多行的键,dataType和值。
^(.*?)[ ]{4}.*?[ ]{4}(.*)$
You can simply use this and grab the captures or groups. 您可以简单地使用它并捕获捕获或分组。
See demo. 参见演示。
https://regex101.com/r/wX9fR1/26 https://regex101.com/r/wX9fR1/26
String line = "test_string";
Pattern pattern = Pattern.compile("^(.*?)[ ]{4}.*?[ ]{4}(.*)$",Pattern.MULTILINE);
Matcher matcher = pattern.matcher(line);
while (matcher.find()) {
System.out.println("group 1: " + matcher.group(1));
System.out.println("group 2: " + matcher.group(2));
}
This will also work : 这也将起作用:
public static void main(String[] args) throws IOException {
String s = "PYTHON_HOME REG_SZ C:\\Python27;C:\\Python27\\Scripts\nPYTHON_PATH REG_SZ C:\\tsde\\Python\\v34\nSCALA_HOME REG_SZ C:\\Program Files (x86)\\scala\nSZ REG_SZ C:\\Program Files\\7-Zip\nTEMP REG_EXPAND_SZ %USERPROFILE%\\AppData\\Local\\Temp\nTMP REG_EXPAND_SZ %USERPROFILE%\\AppData\\Local\\Temp";
System.out.println(s);
Pattern p = Pattern.compile("(?<=\\n|^)(.*?)\\s+(.*?)\\s+(.*?)(?=\\n+|$)",
Pattern.DOTALL);
Matcher m = p.matcher(s);
List<List<String>> list = new ArrayList<List<String>>();
while (m.find()) {
List<String> temp = new ArrayList<String>();
temp.add(m.group(1));
temp.add(m.group(2));
temp.add(m.group(3));
list.add(temp);
}
for (List<String> ll : list) {
System.out.println("1 : " + ll.get(0));
System.out.println("2 : " + ll.get(1));
System.out.println("3 : " + ll.get(2));
}
}
O/P : O / P:
PYTHON_HOME REG_SZ C:\Python27;C:\Python27\Scripts
PYTHON_PATH REG_SZ C:\tsde\Python\v34
SCALA_HOME REG_SZ C:\Program Files (x86)\scala
SZ REG_SZ C:\Program Files\7-Zip
TEMP REG_EXPAND_SZ %USERPROFILE%\AppData\Local\Temp
TMP REG_EXPAND_SZ %USERPROFILE%\AppData\Local\Temp
1 : PYTHON_HOME
2 : REG_SZ
3 : C:\Python27;C:\Python27\Scripts
1 : PYTHON_PATH
2 : REG_SZ
3 : C:\tsde\Python\v34
1 : SCALA_HOME
2 : REG_SZ
3 : C:\Program Files (x86)\scala
1 : SZ
2 : REG_SZ
3 : C:\Program Files\7-Zip
1 : TEMP
2 : REG_EXPAND_SZ
3 : %USERPROFILE%\AppData\Local\Temp
1 : TMP
2 : REG_EXPAND_SZ
3 : %USERPROFILE%\AppData\Local\Temp
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.