Using question marks and stars in strings

I just came accross the following problem: suppose you have a string variable (stringvar) which contains text and question marks. Question marks are usually used as wildcards for single letters or numbers. So, when you want to apply some changes to the variable (here: removing the “?”), you should NOT type

replace stringvar = regexr(stringvar,"?","")
or
replace stringvar = regexr(stringvar,"`?'","")

In this case, any character of stringvar would be replaced. The variable would have no observations. Instead you have to add a backslash right in front the question mark:

replace stringvar = regexr(stringvar,"\?","")

One thought on “Using question marks and stars in strings”

  1. Nick Cox gave me a hint that the first option

    replace stringvar = regexr(stringvar,”?”,””)

    actually does the job pretty well. The “solution” mentioned above, however, is still valid if you want to replace a backslash (“\”). In this case, use “\\”.

Leave a Reply