import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
public static void main(String[] args) {
final String regex = "(?<IntKey>[^\\s<>\\{\\}]+)\\s*<(?<IntValue>\\d+)>|\n"
+ "(?<TrueKey>[^\\s<>\\{\\}]+)\\s*<yes>|\n"
+ "(?<FalseKey>[^\\s<>\\{\\}]+)\\s*<no>|\n"
+ "(?<StrKey>[^\\s<>\\{\\}]+)\\s*<(?<StrValue>.*?)>|\n"
+ "(?<ObjectBegin>[^\\s<>\\{\\}]+)\\s*\\{|\n"
+ "(?<ObjectEnd>\\})|\n"
+ "(?<KeyOnly>[^\\s<>\\{\\}]+)|\n"
+ "(?<Invalid>\\S+)";
final String string = "servername {\n"
+ " store { \n"
+ " servers {\n"
+ " * {\n"
+ " value<>\n"
+ " port<>\n"
+ " folder<C:\\windows> monitor<yes>\n"
+ " args<-T -H>\n"
+ " xrg<store>\n"
+ " wysargs<-t -g -b>\n"
+ " accept_any<yes>\n"
+ " pdu_length<23622>\n"
+ " }\n"
+ " test1 {\n"
+ " name<test1>\n"
+ " port<123>\n"
+ " root<c:\\test>\n"
+ " monitor<yes>\n"
+ " }\n"
+ " test2 {\n"
+ " name<test2>\n"
+ " port<124>\n"
+ " root<c:\\test>\n"
+ " monitor<yes>\n"
+ " }\n"
+ " test3 {\n"
+ " name<test3>\n"
+ " port<125>\n"
+ " root<c:\\test>\n"
+ " monitor<yes>\n"
+ " }\n"
+ " }\n"
+ " senders\n"
+ " timeout<30>\n"
+ " }\n"
+ "}\n";
final Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE | Pattern.COMMENTS);
final Matcher matcher = pattern.matcher(string);
while (matcher.find()) {
System.out.println("Full match: " + matcher.group(0));
for (int i = 1; i <= matcher.groupCount(); i++) {
System.out.println("Group " + i + ": " + matcher.group(i));
}
}
}
}
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for Java, please visit: https://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html