improve pattern matching so $ can be used to detect terminating strings.