import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
public static void main(String[] args) {
final String regex = "^@([^\\s]+).+global\\s+([^\\s]+)\\s+([^,]+).+";
final String string = "; ModuleID = 'foo.c'\n"
+ "source_filename = \"foo.c\"\n"
+ "target datalayout = \"e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128\"\n"
+ "target triple = \"x86_64-pc-linux-gnu\"\n\n"
+ "@Test = dso_local global i32 5, align 4\n\n"
+ "; Function Attrs: noinline nounwind optnone uwtable\n"
+ "define dso_local void @main() #0 {\n"
+ " ret void\n"
+ "}\n\n"
+ "attributes #0 = { noinline nounwind optnone uwtable \"correctly-rounded-divide-sqrt-fp-math\"=\"false\" \"disable-tail-calls\"=\"false\" \"frame-pointer\"=\"all\" \"less-precise-fpmad\"=\"false\" \"min-legal-vector-width\"=\"0\" \"no-infs-fp-math\"=\"false\" \"no-jump-tables\"=\"false\" \"no-nans-fp-math\"=\"false\" \"no-signed-zeros-fp-math\"=\"false\" \"no-trapping-math\"=\"false\" \"stack-protector-buffer-size\"=\"8\" \"target-cpu\"=\"x86-64\" \"target-features\"=\"+cx8,+fxsr,+mmx,+sse,+sse2,+x87\" \"unsafe-fp-math\"=\"false\" \"use-soft-float\"=\"false\" }\n\n"
+ "!llvm.module.flags = !{!0}\n"
+ "!llvm.ident = !{!1}\n\n"
+ "!0 = !{i32 1, !\"wchar_size\", i32 4}\n"
+ "!1 = !{!\"clang version 10.0.0-4ubuntu1 \"}\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