Skip to content
Prev 289892 / 398498 Next

how to match exact phrase using gsub (or similar function)

In most regexs the carrot( ^ ) signifies the start of a line and the
dollar sign ( $ ) signifies the end.

gsub('^S S', 'S', a)

gsub('^S S', 'S', '3421 BIGS St')

you can use logical or inside your pattern too:

gsub('^S S|S S$| S S ', 'S', a)

the " S S " condition is difficult.

gsub('^S S|S S$| S S ', 'S', 'foo S S bar')

gives the wrong output. as does:

gsub('^S S | S S$| S S ', ' S ', 'foo S S bar')
gsub('^S S | S S$| S S ', ' S ', a)


so you might have to catch that with a second gsub.

gsub(' S S ', ' S ', 'foo S S bar')
On Wed, Mar 28, 2012 at 12:32 PM, Markus Weisner <r at themarkus.com> wrote: