in reply to Need to append and replace for % sign in a variable string

Have you actually tested your string replacements? If you look at perlre, you will find that ^foo+ will only match foo, fooo and so on, but never foobar.

Please show a self-contained short example that demonstrates your problem. The code as you posted it does not work, so we can't really help you there.

It might be almost enough to just replace every % with (?:.*), but you will still need to understand how to deal with an SQL string that already contains * for example.