program A {
int x = 10;
tuple date {
int day;
int month;
int year;
}
}
function B {
int y = 20;
...
}
process C {
more code;
}
I'd like to extract whatever is inside the curly braces following A, B, C. I write the following code, but it does not work.
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));
}
}
}
Anyone could tell what I didn't get right?
I've tested the regular expression in Javascript and it worked. See here .
try
Pattern p = Pattern.compile("\\{(.*?)\\}(?!\\s*\\})\\s*", Pattern.DOTALL);
Matcher m = p.matcher(input);
while (m.find()) {
System.out.println(m.group(1));
}
output
int x = 10;
tuple date {
int day;
int month;
int year;
}
int y = 20;
...
more code;
still I think this would be more reliable
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));
}
}
try this:
Pattern p = Pattern.compile("(program|function|process).*?(\\{.*?\\})\\s*", Pattern.DOTALL);
Matcher m = p.matcher(input);
while(m.find()) {
System.out.println(m.group(2));
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.