import re
regex = re.compile(r"simulate\(\w+, \{\n\s+type: \"(\w+)\",\n\s+target: \"\[data-test-id=([\w-]+)\]\",\n\s+\}\);", flags=re.MULTILINE)
test_str = (" simulate(component, {\n"
" type: \"click\",\n"
" target: \"[data-test-id=filterMenu-owner]\",\n"
" });\n\n"
" simulate(component, {\n"
" type: \"click\",\n"
" target: \"[data-test-id=filterMenu-ownerUser]\",\n"
" });\n\n"
" simulate(filterMenuPopup, {\n"
" type: \"click\",\n"
" target: \"[data-test-id=filterMenu-author]\",\n"
" });\n")
subst = "userEvent.$1(screen.getByTestId(\"$2\"))"
result = regex.sub(subst, test_str)
if result:
print(result)
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