Solve regular expression matching problem in leetcode, several edge cases have to be handled correctly.