optimize regex and string processing