import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
public static void main(String[] args) {
final String regex = "@font-face\\s* # Match @font-face and some spaces\n"
+ "( # Start group 1\n"
+ " \\{ # Match \\{\n"
+ " (?: # A non-capturing group\n"
+ " [^\\{\\}]+ # Match anything except \\{\\} one or more times\n"
+ " | # Or\n"
+ " (?1) # Recurse/rerun the expression of group 1\n"
+ " )* # Repeat 0 or more times\n"
+ " \\} # Match \\}\n"
+ ") # End group 1\n";
final String string = ".someclass0 {\n"
+ " background: url('http://images/someimage.png') no-repeat;\n"
+ "}\n\n"
+ "@font-face {\n"
+ " font-family: 'FontAwesome';\n"
+ " src: url(\"fonts/fontawesome-webfont.eot?v=4.0.3\");\n"
+ " src: url(\"fonts/fontawesome-webfont.eot?#iefix&v=4.0.3\") format(\"embedded-opentype\"), url(\"fonts/fontawesome-webfont.woff?v=4.0.3\") format(\"woff\"), url(\"fonts/fontawesome-webfont.ttf?v=4.0.3\") format(\"truetype\"), url(\"fonts/fontawesome-webfont.svg?v=4.0.3#fontawesomeregular\") format(\"svg\");\n"
+ " font-weight: normal;\n"
+ " font-style: normal;\n"
+ "}\n\n"
+ ".someclass1 {\n"
+ " background: url('images/someimage.png') no-repeat;\n"
+ "}\n\n"
+ ".someclass2 {\n"
+ " background: url(\"images/someimage.png\") no-repeat;\n"
+ "}\n\n"
+ ".someclass3 {\n"
+ " background: url(images/someimage.png) no-repeat;\n"
+ "}";
final Pattern pattern = Pattern.compile(regex, Pattern.COMMENTS);
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