As @koshke noted, a very similar question has been answered before (by me). ...But that was grep
and this is gsub
, so I'll answer it again:
"<" is an escape sequence for the beginning of a word, and ">" is the end. In R strings you need to double the backslashes, so:
txt <- "a patterned layer within a microelectronic pattern."
txt_replaced <- gsub("\<pattern\>","form",txt)
txt_replaced
# [1] "a patterned layer within a microelectronic form."
Or, you could use
instead of <
and >
.
matches a word boundary so it can be used at both ends>
txt_replaced <- gsub("\bpattern\b","form",txt)
Also note that if you want to replace only ONE occurrence, you should use sub
instead of gsub
.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…