Java正则表达式匹配大括号之间的文本

时间:2013-01-27 03:45:58

标签: java regex

program A {
   int x = 10;
   tuple date {
            int day;
            int month;
            int year;
   }
}

function B {
    int y = 20;
    ...
}

process C {
    more code;
}

我想提取A,B,C之后花括号内的任何内容。我编写以下代码,但它不起作用。

public class Test {
    public static void main(String[] args) throws IOException {
        String input = FileUtils.readFileToString(new File("input.txt"));
        System.out.println(input);
        Pattern p = Pattern.compile("(program|function|process).*?\\{(.*?)\\}\n+(program|function|process)", Pattern.DOTALL);
        Matcher m = p.matcher(input);
        while(m.find()) {
            System.out.println(m.group(1));
        }
    }
}

任何人都能说出我没有做对的事情?

我已经在Javascript中测试了正则表达式,但它确实有效。请参阅here

2 个答案:

答案 0 :(得分:1)

    Pattern p = Pattern.compile("\\{(.*?)\\}(?!\\s*\\})\\s*", Pattern.DOTALL);
    Matcher m = p.matcher(input);
    while (m.find()) {
        System.out.println(m.group(1));
    }

输出

   int x = 10;
   tuple date {
            int day;
            int month;
            int year;
   }


    int y = 20;
    ...


    more code;

我认为这样会更可靠

    for (int i = 0, j = 0, n = 0; i < input.length(); i++) {
        char c = input.charAt(i);
        if (c == '{') {
            if (++n == 1) {
                j = i;
            }
        } else if (c == '}' && --n == 0) {
            System.out.println(input.substring(j + 1, i));
        }
    }

答案 1 :(得分:0)

试试这个:

Pattern p = Pattern.compile("(program|function|process).*?(\\{.*?\\})\\s*", Pattern.DOTALL);
Matcher m = p.matcher(input);
while(m.find()) {
      System.out.println(m.group(2));
}