import re
regex = re.compile(r"IllegalArgumentException\n\s*at\s+(.+)(?:\n\s*at(?!\s*com\.test).*)*\n\s*at\s+((com\.test[^(\n]*).*)(?:\n\s*at.*)*\n\s*at\s+(?!\3)(com\.test.*)", flags=re.MULTILINE)
test_str = ("IllegalArgumentException\n\n"
" at org.eclipse.jface.action.ActionContributionItem.handleWidgetSelection(ActionContributionItem.java:584)\n"
" at java.security.AccessController.doPrivileged(Native Method)\n"
" at org.eclipse.ui.internal.Workbench.runEventLoop(Workbench.java:2640)\n"
" at org.eclipse.ui.internal.Workbench.runUI(Workbench.java:2604)\n"
" at com.test.package1.user1.client1.client1.Call.invoke(Call.java:90)\n"
" at org.eclipse.ui.internal.Workbench.access$4(Workbench.java:2438)\n"
" at org.eclipse.ui.internal.Workbench$7.run(Workbench.java:671)\n"
" at com.test.package1.user1.client1.client1.Call.invoke(SrvCall.java:84)\n"
" at org.eclipse.ui.internal.Workbench.runEventLoop(Workbench.java:2640)\n"
" at java.security.AccessController.doPrivileged(Native Method)\n"
" at com.test.package2.user2.client2.client2.Call.invoke(Call.java:90)\n"
" at org.eclipse.ui.internal.Workbench.runEventLoop(Workbench.java:2640)\n"
" at org.eclipse.ui.internal.Workbench.runUI(Workbench.java:2604)\n"
" at org.eclipse.ui.internal.Workbench.access$4(Workbench.java:2438)\n"
" at org.eclipse.ui.internal.Workbench$7.run(Workbench.java:671)\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