#include <MsgBoxConstants.au3> ; to declare the Constants of MsgBox
Local $sRegex = "(?m)<ref\s+((?:group|follow|extends)\s*=(?:(?!name\s*=)[\s\S])*)?name\s*=\s*(?:"\s*([^"](?:(?!\s*\/>|\s*"\s*>|\s+(?:group|follow|extends)).)*?)\s*"|'\s*([^'"](?:(?!\s*\/>|\s*'>|\s+(?:group|follow|extends)).)*?)\s*'|([^"](?:(?!\s*\/>|\s*>|\s+(?:group|follow|extends)).)*))(\s+(?:group|follow|extends)\s*=(?:(?!\s*\/>|"\s*>|'\s*>)[\s\S])*)*\s*(?:(\/)|)>"
Local $sString = "<ref name=Test1 /><ref name=Test2 />" & @CRLF & _
"<ref name=Bad 1 /><ref name=Bad 2 />" & @CRLF & _
"<ref name="Test 3"/>" & @CRLF & _
"<ref name="Test 4" />" & @CRLF & _
"<ref name=Test5>Foo</ref>" & @CRLF & _
"<ref name="Test 6">Foo</ref>" & @CRLF & _
"<ref " & @CRLF & _
" name=Test7>Foo" & @CRLF & _
"</ref>" & @CRLF & _
"<ref " & @CRLF & _
" name = Test8 >" & @CRLF & _
" Foo" & @CRLF & _
"</ref> " & @CRLF & _
"<ref name = Test9 > Foo </ref>" & @CRLF & _
"<ref " & @CRLF & _
" name="Test 10">Foo" & @CRLF & _
"</ref>" & @CRLF & _
"<ref " & @CRLF & _
" name = "Test 11" >" & @CRLF & _
" Foo" & @CRLF & _
"</ref>" & @CRLF & _
"<ref name = Test12 > Foo </ref>" & @CRLF & _
"<ref name = Test13 />" & @CRLF & _
"<ref name = "Test 14" />" & @CRLF & _
"<ref " & @CRLF & _
" name = Test15 />" & @CRLF & _
"<ref" & @CRLF & _
" name =" & @CRLF & _
" "Test 16"" & @CRLF & _
"/>" & @CRLF & _
"<ref" & @CRLF & _
" name" & @CRLF & _
" =" & @CRLF & _
" Test17" & @CRLF & _
" />" & @CRLF & _
"<ref" & @CRLF & _
" name" & @CRLF & _
" =" & @CRLF & _
" "Test 18"" & @CRLF & _
" />" & @CRLF & _
"<ref name="Test/19" />" & @CRLF & _
"==A heading==" & @CRLF & _
"<ref name = "Test/20" />" & @CRLF & _
"<ref name = "Test 21"/>" & @CRLF & _
"<ref name = "Test / 22"/>" & @CRLF & _
"<ref name = "Test > 23"/>" & @CRLF & _
"<ref name="Te>st/ 23.1"/>" & @CRLF & _
"<ref" & @CRLF & _
" name" & @CRLF & _
" ="" & @CRLF & _
" Test 24" & @CRLF & _
" "/>" & @CRLF & _
"<ref name='Test/ 25' />" & @CRLF & _
"<ref name='Test/ 26'> Foo </ref>" & @CRLF & _
"<ref name='Te>st/ 27' />" & @CRLF & _
"<ref name = " Test 28 " >Foo</ref>" & @CRLF & _
"<ref name='Test "29"' /> - This one is expected to produce invalid XML "'Test "29"'"; surprisingly, MW can parse it anyway for a case this simple, but not if there's a space within the interior-most quotes: "'Test "2 9"'"" & @CRLF & _
"<ref name = "Test 30" >" & @CRLF & _
"<ref name = "Test 31">" & @CRLF & _
"<ref name = "Test 32" />" & @CRLF & _
"<ref name = "Test 33"/>" & @CRLF & _
"<ref name = Test34 >" & @CRLF & _
"<ref name = Test35>" & @CRLF & _
"<ref name = Test36 />" & @CRLF & _
"<ref name = Test37/>" & @CRLF & _
"<ref name = " Test 38 "/>" & @CRLF & _
"<ref name = " Test 39 " />" & @CRLF & _
"<ref name='Test 40'/>" & @CRLF & _
"<ref name=AAA/>" & @CRLF & _
"<ref name="BBB AAA BBB" />" & @CRLF & _
"<ref name="This "isjustnot" valid"/> - This one is expected to produce invalid XML "This "isjustnot" valid"; surprisingly, MW can parse it anyway for a case this simple, but not if there's a space within the interior-most quotes: "This "is just not" valid"." & @CRLF & _
"<ref name="This 'actually is' valid"/>" & @CRLF & _
"<ref name='But this "is also" a problem' /> (or will be after our " substitution)" & @CRLF & _
"<ref name=apostrophe's />" & @CRLF & _
"<ref name="more apostrophe's" />" & @CRLF & _
"<ref name='this's bad, but we can fix it!' />" & @CRLF & _
"<ref name='this is okay'/>" & @CRLF & _
"<ref name="'this is dumb'"/>" & @CRLF & _
"<ref name="this is 'less' dumb" />" & @CRLF & _
"<ref name="what about 'this case'"/>" & @CRLF & _
"<ref name="'or this' one" />" & @CRLF & _
"" & @CRLF & _
"<ref name="foo" group="bar"/>" & @CRLF & _
"<ref name="foo" group="bar" />" & @CRLF & _
"<ref group="bar" name="foo" />" & @CRLF & _
"<ref name='foo' group='bar'/>" & @CRLF & _
"<ref name='foo' group='bar' />" & @CRLF & _
"<ref group='bar' name='foo' />" & @CRLF & _
"<ref name=foo group=bar/>" & @CRLF & _
"<ref name=foo group=bar />" & @CRLF & _
"<ref group=bar name=foo/>" & @CRLF & _
"<ref name="foo" follow="bar" />" & @CRLF & _
"<ref name='foo' follow='bar'/>" & @CRLF & _
"<ref name=foo follow=bar/>" & @CRLF & _
"<ref name="foo" extends="bar" />" & @CRLF & _
"<ref name='foo' extends='bar'/>" & @CRLF & _
"<ref name=foo extends=bar/>" & @CRLF & _
"<ref name='foo' group="bar" extends=baz follow="quux quux" />" & @CRLF & _
"<ref group="bar" name='foo' extends=baz follow="quux quux" />" & @CRLF & _
"<ref group="bar" extends=baz name='foo' follow="quux quux" />" & @CRLF & _
"<ref group="bar" extends=baz follow="quux quux" name='foo' />" & @CRLF & _
"<ref group="bar" extends=baz name=foo follow="quux quux" />" & @CRLF & _
"<ref group="bar" extends=baz name="foo's foo" follow="quux quux" />" & @CRLF & _
"<ref group="bar's > / bar" extends=baz name='foos > / foo' follow="quux quux" />" & @CRLF & _
"<ref group=" & @CRLF & _
" "bar's > / bar"" & @CRLF & _
" extends=" & @CRLF & _
" baz" & @CRLF & _
" name=" & @CRLF & _
" '" & @CRLF & _
" foos > / foo" & @CRLF & _
" '" & @CRLF & _
" follow=" & @CRLF & _
" "quux > quux"" & @CRLF & _
" />" & @CRLF & _
"<ref name="foos > / foo" group="bar's > / bar" extends=baz follow="quux quux"/>" & @CRLF & _
"<ref " & @CRLF & _
" name = " & @CRLF & _
" '" & @CRLF & _
" foos > / foo" & @CRLF & _
" '" & @CRLF & _
" group = " & @CRLF & _
" "bar's > / bar"" & @CRLF & _
" extends" & @CRLF & _
" =baz" & @CRLF & _
" follow" & @CRLF & _
" ="" & @CRLF & _
" quux quux" & @CRLF & _
" "" & @CRLF & _
" />" & @CRLF & _
"<ref name="foos > / foo" group="bar's > / bar" extends=baz follow='quux quux'/>" & @CRLF & _
""
Local $sSubst = "<ref $1name="$2$3$4"$5${6:+ /:}>"
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