import re
regex = re.compile(r"buffer_1", flags=re.MULTILINE)
test_str = ("/*\n"
" * To change this license header, choose License Headers in Project Properties.\n"
" * To change this template file, choose Tools | Templates\n"
" * and open the template in the editor.\n"
" */\n"
"package kythuatlaptrinhat;\n\n"
"import java.util.Random;\n"
"import java.util.Scanner;\n\n"
"/**\n"
" *\n"
" * @author shadyside\n"
" */\n"
"public class Bai2Lab1 {\n\n"
" public static void main(String[] args) {\n"
" Scanner input = new Scanner(System.in);\n\n"
" System.out.println(\"Nhap chieu dai mang:\");\n"
" int inputLen = input.nextInt();\n"
" int[] inputArray = inputArray(inputLen);\n\n"
" System.out.println(\"Mảng nguồn: \");\n"
" printArray(inputArray);\n"
" System.out.println(\"Nhập điểm bắt đầu sao chép: \");\n"
" int start = input.nextInt();\n"
" System.out.println(\"Nhập điểm kết thúc: \");\n"
" int end = input.nextInt();\n"
" System.out.println(\"Nhập chiều dài mảng đích\");\n"
" int outputLen = input.nextInt();\n"
" int[] ouputArray = inputArray(outputLen);\n\n"
" System.out.println(\"Nhập vị trí bắt đầu thêm dữ liệu\");\n"
" int startOfCopy = input.nextInt();\n\n"
" System.out.println(\"Mảng đích ban đầu: \");\n"
" printArray(ouputArray);\n"
" System.out.println(\"-------------------------\");\n"
" System.out.println(\"Ket qua:\");\n"
" System.out.println(\"Mảng nguồn: \");\n"
" printArray(inputArray);\n"
" int[] result = copyArray(inputArray, inputLen, start, end, ouputArray, outputLen, startOfCopy);\n"
" if (result == null) {\n"
" System.out.println(\"Có lỗi xảy ra\");\n"
" } else {\n"
" System.out.println(\"Mảng đích: \");\n"
" printArray(result);\n"
" }\n"
" }\n\n"
" public static int[] copyArray(int[] inputArray, int inputLen, int start, int end, int[] outputArray, int outpuLen, int startOfCopy) {\n"
" if (start >= 0 && end <= inputLen && startOfCopy >= 0 && startOfCopy <= outpuLen && start <= end) {\n"
" /*\n"
" *Trường hợp số lượng cần copy trong mảng nguồn nhỏ hơn số phần tử còn trống trong mảng đích\n"
" */\n"
" if ((end - start + 1) < (outpuLen - startOfCopy - 1)) {\n"
" /*\n"
" * Thực hiện dịch các phần tử\n"
" */\n"
" for (int i = outpuLen; i > startOfCopy + (end - start + 1) + 1; i--) {\n"
" outputArray[i - 1] = outputArray[i - 1 - (end - start + 1)];\n"
" }\n"
" /*\n"
" * Copy các phần tử vào mảng đích\n"
" */\n"
" for (int i = start; i <= end; i++) {\n"
" outputArray[startOfCopy + 1 + (i - start)] = inputArray[i];\n"
" }\n"
" System.out.println(\"So gia tri duoc sao chep: \" + (end - start + 1));\n"
" }\n"
" /*\n"
" *Trường hợp số lượng cần copy trong mảng nguồn bằng số phần tử còn trống trong mảng đích\n"
" */\n"
" else if ((end - start + 1) == (outpuLen - startOfCopy - 1)) {\n"
" for (int i = start; i <= end; i++) {\n"
" outputArray[startOfCopy + (i - start)] = inputArray[i];\n"
" }\n"
" } \n"
" /*\n"
" *Trường hợp số lượng cần copy trong mảng nguồn lớn hơn số phần tử còn trống trong mảng đích\n"
" */\n"
" else {\n"
" for (int i = start; i < start + (outpuLen - startOfCopy); i++) {\n"
" outputArray[startOfCopy + (i - start)] = inputArray[i];\n"
" }\n"
" }\n"
" }\n"
" return outputArray;\n"
" }\n\n"
" public static int[] inputArray(int len) {\n"
" int[] inputArray = new int[len];\n"
" Random random = new Random();\n"
" for (int i = 0; i < len; i++) {\n"
" inputArray[i] = random.nextInt(100);\n"
" }\n"
" return inputArray;\n"
" }\n\n"
" public static void printArray(int[] a) {\n"
" for (int i = 0; i < a.length; i++) {\n"
" System.out.print(a[i] + \" \");\n"
" }\n"
" System.out.println(\"\");\n"
" }\n\n"
"}\n")
matches = regex.finditer(test_str)
for match_num, match in enumerate(matches, start=1):
print(f"Match {match_num} was found at {match.start()}-{match.end()}: {match.group()}")
for group_num, group in enumerate(match.groups(), start=1):
print(f"Group {group_num} found at {match.start(group_num)}-{match.end(group_num)}: {group}")
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 Python, please visit: https://docs.python.org/3/library/re.html