import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
public static void main(String[] args) {
final String regex = "value\\s+(one|two|three)\\s+=\\s+([0-9]+)";
final String string = "Statistics indicator:0x222235\n\n"
+ "number of records = 3\n\n"
+ "records[0]\n\n"
+ "value one = 2\n\n"
+ "value two = 5\n\n"
+ "blocks = 2\n\n"
+ "block[0] {\n\n"
+ "some irrelevant data....\n\n"
+ "value three = 4 bytes\n\n"
+ "}\n\n"
+ "block[1]{\n\n"
+ "some irrelevant data...\n\n"
+ "value three = 6 bytes\n\n"
+ "}\n\n"
+ "records[1]\n\n"
+ "value one = 3\n\n"
+ "value two = 5\n\n"
+ "blocks = 1\n\n"
+ "block[0] {\n\n"
+ "some irrelevant data....\n\n"
+ "value three = 4 bytes\n\n"
+ "}\n\n"
+ "records[2]\n\n"
+ "value one = 7\n\n"
+ "value two = 6\n\n"
+ "blocks = 2\n\n"
+ "block[0] {\n\n"
+ "some irrelevant data....\n\n"
+ "value three = 3 bytes\n\n"
+ "}\n\n"
+ "block[1]{\n\n"
+ "some irrelevant data...\n\n"
+ "value three = 4 bytes\n\n"
+ "}\n\n"
+ "Statistics indicator:0x135256\n\n"
+ "number of records = 2\n\n"
+ "records[0]\n\n"
+ "value one = 4\n\n"
+ "value two = 8\n\n"
+ "blocks = 1\n\n"
+ "block[0] {\n\n"
+ "some irrelevant data....\n\n"
+ "value three = 6 bytes\n\n"
+ "}\n\n"
+ "records[1]\n\n"
+ "value one = 3\n\n"
+ "value two = 5\n\n"
+ "blocks = 1\n\n"
+ "block[0] {\n\n"
+ "some irrelevant data....\n\n"
+ "value three = 3 bytes\n\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