Improved regex matching