Solving grammer using regex patterns