#include <MsgBoxConstants.au3> ; to declare the Constants of MsgBox
Local $sRegex = "(?mx)^(?:ALTER\ TABLE\ ONLY\ )" & @CRLF & _
"([^\n\r]+)[\n\r]" & @CRLF & _
"(?:ADD\ CONSTRAINT\ )" & @CRLF & _
"fk_\S+(?=.*REFERENCES\ ([^()]+))"
Local $sString = "ALTER TABLE ONLY document_labels" & @CRLF & _
"ADD CONSTRAINT fk_g71qgs6l2ufr3170u44j5fpk3 FOREIGN KEY (label_id) REFERENCES application_value(id);" & @CRLF & _
"ALTER TABLE ONLY rule_group_functionality_mapping" & @CRLF & _
"ADD CONSTRAINT fk_g8twyunj9bm096sqywdi8rcx8 FOREIGN KEY (rule_group) REFERENCES application_value(id);" & @CRLF & _
"ALTER TABLE ONLY time_track" & @CRLF & _
"ADD CONSTRAINT fk_gmpyguqbpm1ndjjsxvt03wq4g FOREIGN KEY (user_id) REFERENCES user_account(user_id);" & @CRLF & _
"" & @CRLF & _
"" & @CRLF & _
"" & @CRLF & _
"ALTER TABLE ONLY document_labels" & @CRLF & _
"ADD CONSTRAINT fk_document_labels_application_value FOREIGN KEY (label_id)" & @CRLF & _
"REFERENCES application_value(id);"
Local $sSubst = "ALTER TABLE $1\nADD CONSTRAINT fk_$1_$2"
Local $sResult = StringRegExpReplace($sString, $sRegex, $sSubst)
MsgBox($MB_SYSTEMMODAL, "Result", $sResult)
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 AutoIt, please visit: https://www.autoitscript.com/autoit3/docs/functions/StringRegExp.htm