# coding=utf8
# the above tag defines encoding for this document and is for Python 2.x compatibility
import re
regex = r"(?P<first>^(?P<intend>\s*?)diagGetParameterRaw.+?(?P<diagresp>\w.+?),.*?\"SecurityClass\", (?P<first_rest>.+?;(?P<newline>[\n\r]+)))(?P<second>(?P<second_start>\s*?_DiagGenerateKeyFromSeed.+.?buffer,).+?(?P<second_rest>0x\d+?,.+))"
test_str = ("// diagGetParameterRaw(transition_1_2_0_Resp, \"SecurityClass\", buffer, 4096);\n"
"// _DiagGenerateKeyFromSeed (buffer, 1, 0x11, \"BR120x\", \"\", op1, 6, _length); length = 6;\n"
"*******\n"
"diagGetParameterRaw(transition_1_2_0_Resp, \"SecurityClass\", buffer, 4096);\n"
"_DiagGenerateKeyFromSeed (buffer, 1, 0x11, \"BR120x\", \"\", op1, 6, _length); length = 6;\n"
" diagGetParameterRaw(transition_1_2_0_Resp, \"SecurityClass\", buffer, 4096);\n"
" _DiagGenerateKeyFromSeed (buffer, 1, 0x11, \"BR120x\", \"\", op1, 6, _length); length = 6;\n"
"****************\n"
" RawDiagGetParameterRaw(transition_1_0_0_Resp, 16, 64, buffer, 4096);\n"
" _DiagGenerateKeyFromSeed (buffer, 8, 0x11, \"BR120x\", \"\", op1, 6, _length); length = 6;\n")
subst = "\\g<newline>\\g<intend>\\g<newline>//\\g<intend>removed the next two lines as diva uses only one of our three parameters\\g<newline>//\\g<first>//\\g<second>\\g<newline>//\\g<intend>\\g<newline>//\\g<intend>this works correctly:\\g<newline>\\g<intend>\\g<newline>\\g<intend>RawDiagGetParameterRaw ( \\g<diagresp>, 16, 64, \\g<first_rest>\\g<second_start> 8, \\g<second_rest>\\g<newline>\\g<newline>//\\g<intend>replacement ends here\\g<newline>\\g<intend>"
# You can manually specify the number of replacements by changing the 4th argument
result = re.sub(regex, subst, test_str, 0, re.MULTILINE | re.IGNORECASE)
if result:
print (result)
# Note: for Python 2.7 compatibility, use ur"" to prefix the regex and u"" to prefix the test string and substitution.
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