Check if it is a valid string before appending it

Check if the string to be added will create a valid expression, if it
does it will create the new string. If it will not it returns the
original string.