Matching entire string in R

前端 未结 3 933
陌清茗
陌清茗 2021-01-25 00:33

Consider the following string:

string = \"I have #1 file and #11 folders\"

I would like to replace the pattern #1 with the word

3条回答
  •  遥遥无期
    2021-01-25 01:34

    If you use the perl=TRUE argument to tools like gsub then the perl regex engine will be used which has some options that could help.

    The pattern "#1\\b" will match #1 followed by a word boundary, so it would match #1, but not #11 (since there is no boundary between the 2 1's). There are also tools for positive and negative look ahead which look for things following your pattern (like the word file maybe), but does not include them in the part to be replaced.

提交回复
热议问题