#include <MsgBoxConstants.au3> ; to declare the Constants of MsgBox
Local $sRegex = "(\bONLY\h+)(.+)(\R.*?fk)_\S+(.+?\bREFERENCES\h+)([^(]+)"
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);"
Local $sSubst = "$1$2$3_$2_$5$4$5"
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