import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
public static void main(String[] args) {
final String regex = "^\\s*?((?:virtual\\s+|const\\s+|friend\\s+|volatile){0,3}\\s*(?:\\w+\\s*::\\s*)?\\s*[\\w]*?)\\s*\\w+\\s*::\\s*(~?[\\w]+)\\s*(\\(.*\\))\\s*(const)?\\s*[\\{|:]";
final String string = "#include <iostream>\n"
+ "#include <iomanip>\n"
+ "#include \"Time.h\"\n"
+ "using namespace std;\n\n"
+ "Time :: ~Time(const int h, const int m, const int s) \n"
+ " : hour(h), minute (m), second(s)\n"
+ "{}\n\n"
+ "virtual const std::void Time :: setTime(const int h, const int m, const int s) \n"
+ "{\n"
+ " hour = h;\n"
+ " minute = m;\n"
+ " second = s; \n"
+ "} \n"
+ " \n"
+ "std::string Time :: print() const\n"
+ "{\n"
+ " cout << setw(2) << setfill('0') << hour << \":\"\n"
+ " << setw(2) << setfill('0') << minute << \":\"\n"
+ " << setw(2) << setfill('0') << second << \"\\n\"; \n"
+ " \n"
+ "}\n"
+ " \n"
+ "bool Time :: equals(const Time &otherTime)\n"
+ "{\n"
+ " if(hour == otherTime.hour \n"
+ " && minute == otherTime.minute \n"
+ " && second == otherTime.second)\n"
+ " return true;\n"
+ " else\n"
+ " return false;\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