Convert Sql LIKE to Regex
I think you're overcomplicating this and your code still doesn't work correctly. The LIKE pattern bcd shouldn't match abcde, but it does with your code.

What you should do is to always add ^ at the start and $ at the end.

This means the following conversions:


bcd → ^bcd$
%bcd → ^.*?bcd$
bcd% → ^bcd.*?$
%bcd% → ^.*?bcd.*?$


In the cases where the pattern starts with %, the ^ is not necessary (and similarly for $ and % at the end), but it also doesn't do any harm.