Regex implementation to algorithim