在JAVA中解析多行字符串,其中包含空格

时间:2015-02-11 06:01:05

标签: java regex

我正在尝试解析在键,数据类型和值之前和之间具有四个空格的字符串

    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

我想解析字符串并将解析后的值分配给变量或多维数组。我正在寻找的预期结果是:

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

到目前为止,我一直在使用java.util.regex中的模式和匹配器,并且实际上并没有获得任何地方。

请注意,给定的字符串块可能包含更多行键,dataType和值。

2 个答案:

答案 0 :(得分:2)

这也有效:

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:

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

答案 1 :(得分:1)

^(.*?)[ ]{4}.*?[ ]{4}(.*)$

您可以简单地使用此功能并抓取捕获或组。

参见演示。

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));
}