import re
regex = re.compile(r"((?:(?P<p1>\((?:[^\(\)]|(?&p1))*\))|(?P<p2>[\w\.]))+)\s*[!=]=\s*((?:(?&p1)|(?&p2))+)", flags=re.MULTILINE)
test_str = ("if (programWorkflowState.getTerminal(1, 2) == Boolean.TRUE) {\n\n"
"Want: programWorkflowState.getTerminal(1, 2) and Boolean.TRUE\n\n"
"boolean ignore = !_isInStatic.isEmpty() && (_isInStatic.peek() == 3) && isAnonymous;\n\n"
"Want: _isInStatic.peek() and 3\n\n"
"boolean b = (num1 * ( 2 + num2)) == value;\n\n"
"Want: (num1 * ( 2 + num2)) and value")
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