import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
public static void main(String[] args) {
final String regex = "(?<=^|[\\n;](?:[\\t ]*(?:\\w+ )?)?)export\\s*default(?:\\s*(?<modifiers>(?:abstract(?= *class)|async(?=[\\S ]))+))?(?:\\s*(?<kind>class|const +enum|enum|function\\*?(?=[ (])|interface|namespace|type(?! *\\{)))?(?:\\s*(?<exports>[$_\\p{ID_Start}][$\\u200C\\u200D\\p{ID_Continue}]*)(?=(?:\\s*[\\n(\\{;])|(?:\\s*=\\s*\\{)|$))?";
final String string = "export default function () { /* … */ }\n"
+ "export default async function() { /* … */ }\n"
+ "export default function* () { /* … */ }\n"
+ "export default async function* () { /* … */ }\n"
+ "export default class { /* … */ }\n\n"
+ "export default async function functionName() { /* … */ }\n"
+ "export default function functionName() { /* … */ }\n"
+ "export default function* generatorName() { /* … */ }\n"
+ "export default async function* generatorNameAsync() { /* … */ }\n"
+ "export default class ClassName { /* … */ }\n"
+ "export default abstract class ClassName { /* … */ }\n"
+ "export default type Options = {}\n\n"
+ "export default async () => {}\n"
+ "export default async arg => {}\n"
+ "export default () => {}\n"
+ "export default arg => {}\n"
+ "export default (arg) => {}\n\n"
+ "export default foo\n"
+ "export default foo;export default foo\n"
+ "export default 1 + 1;\n"
+ "export default {};\n\n"
+ "declare module 'module-name' {\n"
+ " export default class ClassName { /* … */ }\n"
+ "}\n\n"
+ "\"export default type Options = {}\"\n\n"
+ "/*export default foo */\n\n"
+ "/* export default foo */\n\n"
+ "//export default foo\n\n"
+ "// export default foo\n\n"
+ "/**\n"
+ " * Resolvable file extensions.\n"
+ " *\n"
+ " * @example\n"
+ " * export default async function read () {}\n"
+ " */\n\n"
+ "export default type Options = {}";
final Pattern pattern = Pattern.compile(regex, 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