import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
public static void main(String[] args) {
final String regex = "(?P<assign_op>(\\:|\\!|\\?)?=)|(?P<token_subst>$|%(\\<\\@))|(?P<token_ident>[\\w\\d\\.\\,]+)|(?P<target_name_match>%\\.?)|(?P<token_rule_sep>[:])|(?P<incr_op>\\n)|(?P<line_end>\\n)|(?P<comment_op>#[.*]+$)|(?P<adjust_parser>[\\$%\\s\\t\\:])";
final String string = "CC := nasm\n"
+ "BOOT_REQ := text.o\n"
+ "FREELDR_SRCS := freeldr.c\n"
+ "IMG_NAME := boot.img\n\n"
+ "TEXT_SECTION := 7c00\n\n"
+ "# UFS2, ZFS and/or RAW\n"
+ "FILESYSTEMS :=\n"
+ "CPU_FLAGS := PAE\n"
+ "SDK_FLAGS :=\n"
+ "VERSION := \"1.00.00\"\n\n"
+ "%.o:\n"
+ " $(ASM) ${SDK_FLAGS} -f bin -o $@ $(patsubst %.bin,%.asm,$@))\n\n"
+ "${IMG_NAME}:\n"
+ " $(BOOT_REQ) $(FREELDR_SRCS:.c=.o)\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