import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
public static void main(String[] args) {
final String regex = "<!-- @\\[(?<component>[\\w+]+)\\(*(?<classes>[\\w+]+)*\\)*\\] -->";
final String string = "<!-- @[Hero] -->\n\n"
+ "# Creating new contexts\n\n"
+ "<!-- @[UsageExample] -->\n\n"
+ "## Usage example\n\n"
+ "```javascript\n"
+ " Import { ICON_NAME } from 'Icons'\n"
+ "```\n\n"
+ "<!-- @[/Hero] -->\n\n"
+ "<!-- @[ArticleSection] -->\n\n\n"
+ "## Section Title\n\n"
+ "### Section subtitle that contains additional information\n\n"
+ "Cillum ipsum ad veniam elit non. Sunt ea ut quis qui dolore id voluptate magna. Ex non commodo reprehenderit ipsum irure. Ad excepteur nulla ullamco et deserunt magna et sint reprehenderit sint esse commodo. Tempor duis anim nisi commodo incididunt ut ex et sunt laborum excepteur ea culpa laborum.\n\n"
+ "<!-- @[NoteBlock(warning)] -->\n\n"
+ "> #### Be Careful\n"
+ "> Eu laboris eiusmod ut exercitation minim laboris ipsum magna consectetur est [commodo](/nope).\n\n"
+ "<!-- @[/NoteBlock] -->\n\n"
+ "Officia esse Lorem ad duis dolore nostrud ex elit aliqua incididunt sint ad ex. Eiusmod do in ad aute nulla eiusmod tempor Lorem non. Qui sunt voluptate laborum mollit elit adipisicing minim dolore voluptate veniam incididunt proident ullamco. Ipsum est cupidatat occaecat pariatur ut aute.\n\n"
+ "<!-- @[CodeExample] -->\n\n"
+ "#### Code example\n\n"
+ "```javascript\n"
+ " class ScrollingList extends React.Component {\n"
+ " constructor(props) {\n"
+ " super(props);\n"
+ " this.listRef = React.createRef();\n"
+ " }\n\n"
+ " render() {\n"
+ " return (\n"
+ " <div ref={this.listRef}>{/* ...contents... */}</div>\n"
+ " );\n"
+ " }\n"
+ " }\n"
+ "```\n\n"
+ "<!-- @[PropsTable] -->\n\n"
+ "#### Props\n\n"
+ "Prop Name | Value | Required\n"
+ "--------- | ----- | --------\n"
+ "**label** | `{String}` | true\n"
+ "**state** | `default` `secondary` `info` `warning` `danger` | false\n"
+ "**size** | `xs` `s` `m` `l` `xl` | false\n"
+ "**disabled** | `-` | false\n\n"
+ "<!-- @[/ArticleSection] -->\n";
final Pattern pattern = Pattern.compile(regex);
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