local PATTERN = '%f[^ \0]<<([^ ].-)>>%f[ \0] ?' .. msg:gsub(PATTERN, '\1ACTION %1\1')) And it works pretty good, except for e.g. "test <<thing>>" which gets sent as "test \1ACTION thing\1" and I'd rather it be sent as "test\1ACTION thing\1".