import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
public static void main(String[] args) {
final String regex = "[*\\s]* # multiple space or asterisk characters\n"
+ "@ # followed by the @ sign which\n"
+ "(?P<name>[\\w\\\\]+) # has a string of \\w or \\ characters following it\n"
+ "\\s*\\(? # separated by spaces and maybe opening parenthesis\n"
+ " (?P<value> # store the value attached to the named annotation\n"
+ " (?:\n"
+ " [^@]* # value is made of enlisted characters\n"
+ " [^*\\s)] # but does not end with\n"
+ " )\n"
+ " )? # the value is optional parameter\n"
+ "(?:\\s|\\n|\\))";
final String string = "/**\n"
+ " * @Name Test\n"
+ " * @Response /test\n"
+ " * @Right ROLE_ADMIN\n"
+ " * @param Test\n"
+ " */";
final Pattern pattern = Pattern.compile(regex, Pattern.DOTALL | Pattern.COMMENTS | Pattern.MULTILINE | Pattern.UNICODE_CASE);
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