Skip to content

Use of [:alnum:] or . in gsub() etc..

2 messages · ppaarrkk, Marc Schwartz

#
test = c ( "AAABBB", "CCC" )


This works :

gsub ( "[A-Z]", "2", test )



None of these do :

gsub ( "[A-Z]", [:alnum:], test )
gsub ( "[A-Z]", [[:alnum:]], test )
gsub ( "[A-Z]", "[:alnum:]", test )
gsub ( "[A-Z]", "[[:alnum:]]", test )
gsub ( "[A-Z]", "^[:alnum:]$", test )



What am I doing wrong, please ?
#
on 01/16/2009 10:13 AM ppaarrkk wrote:
The syntax ordering is off. You are in effect trying to replace the
characters A-Z with the character vector "[[:alnum:]]". Thus:
[1] "[[:alnum:]][[:alnum:]][[:alnum:]][[:alnum:]][[:alnum:]][[:alnum:]]"
[2] "[[:alnum:]][[:alnum:]][[:alnum:]]"


Recall the basic syntax for gsub() is:

  gsub(SearchPattern, ReplacementVector, Source)


What you want is:
[1] "222222" "222"


HTH,

Marc Schwartz