import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
public static void main(String[] args) {
final String regex = "(?P<first_part>(?:ifstream|ofstream)\\s+[0-9a-zA-z_]+\\s*\\()(?:\\\"(?P<file_name>[0-9a-zA-z_.]+)\\\")";
final String string = "ifstream f1(\"text1.txt\");\n"
+ " ifstream f2 (\"text2.txt\");\n\n"
+ "ifstream f_ASFdf1(\"text1\");\n\n"
+ " ofstream out (\"output.txt\");//создали объект и сразу же открыли файл для записи\n\n\n"
+ "ofstream of;//создали объект класса\n"
+ "of.open(\"test.txt\");//открыли файл для записи\n\n"
+ "ofstream of(\"test.txt\", ios_base::app); //добавление информации к концу файла\n"
+ "ofstream of(\"test.txt\", ios_base::trunc); //очистить содержимое файла\n"
+ "ofstream of(\"test.txt\", ios_base::binary); //открыть в бинарном режиме";
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