Skip to content

Removing "+" and "?" signs

8 messages · Steven Kang, jim holtman, Linlin Yan +4 more

#
'?' is a metacharacter in a regular expression.  You have to escape it:
Error in gsub("?", " ", x) : invalid regular expression '?'
In addition: Warning message:
In gsub("?", " ", x) :
  regcomp error:  'Invalid preceding regular expression'
[1] "asdf+,jkl "
On Sun, Nov 22, 2009 at 6:01 PM, Steven Kang <stochastickang at gmail.com> wrote:

  
    
#
Try this:
gsub("[?]", " ", x)
On Mon, Nov 23, 2009 at 7:01 AM, Steven Kang <stochastickang at gmail.com> wrote:
#
On 23/11/2009, at 12:01 PM, Steven Kang wrote:

            
(a) That's funny.  I don't get an error message when I try your example.
I get

[1] " a s d f + , j k l ? "

Of course that's not what you want, though.

(b) You need to escape the question mark:

  > gsub("\\?", " ", x)

yields

[1] "asdf+,jkl "

which I think *is* what you want.

	cheers,

		Rolf Turner



######################################################################
Attention:\ This e-mail message is privileged and confid...{{dropped:9}}
#
On Nov 22, 2009, at 6:01 PM, Steven Kang wrote:

            
Since both ? and + are special regex characters, to do both the  
substitutions at once you need to use double backslashes and an <or>

 > gsub("\\?|\\+", " ", x)
[1] "asdf ,jkl "
#
Jorge Ivan Velez wrote:
Once you're into character classes ([....]), you might as well go all 
the way:

 > gsub("[?+]", "", x)
[1] "asdf,jkl"