Solution for Regulare expression matching, reference from gongxuns.blogspot.com