import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
public static void main(String[] args) {
final String regex = "(^\\s+(?P<att_name>[^\\s]+)\\s+(?P<att_value>[^\\s\\{]+|\"[^\"]+?\")\\s?(?P<att_body>\\{[\\s\\S]+?\\})?)$";
final String string = "\n"
+ " rel_tgt_id 2\n"
+ " IncomingUser \"user1234 secret123456\"\n"
+ " GROUP access_group{\n"
+ " LUN 0 dev2-0\n"
+ " LUN 1 dev2-1\n"
+ " LUN 2 dev2-2\n"
+ " LUN 3 dev2-3\n"
+ " LUN 4 dev2-4\n"
+ " LUN 5 dev2-5\n"
+ " LUN 6 dev2-6\n"
+ " LUN 7 dev2-7\n"
+ " LUN 8 dev2-8\n"
+ " LUN 9 dev2-9\n"
+ " INITIATOR iqn.1994-09.org.freebsd:*\n"
+ " }\n"
+ " enabled 1\n"
+ " MaxSessions 10\n";
final Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE);
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