2
stringg <- c("You are\ngoing to learn 3 things, the first one is not to extract, and\n2 and 3 are simply digits.", "....", "....",....)

in R Studio I want to find all listitem/strings which ends with a "." and after the dot I want to add a $ sign to it.

grep("\\.$", stringg , value = TRUE) # this gives me the string from list which ends with a dot

with which command can I add the $?

I know I could use str_replace like following but I want to know if I could add instead of replacing it?

str_replace(stringg, "\\.$", ".$")

thanks!

Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
TheDev
  • 153
  • 5

1 Answers1

0

There are a lot of ways to add a char to a string ending with a specific character.

Here are a few with the major three regex engines used in R regex functions:

stringg <- c("You are\ngoing to learn 3 things, the first one is not to extract, and\n2 and 3 are simply digits.", "....", "....")
sub("(\\.)$", "\\1$", stringg)
sub("$(?<=\\.)", "$", stringg, perl=TRUE)
library(stringr)
str_replace(stringg, "\\.$", "\\0$")

See the R demo online.

Details

  • sub("(\\.)$", "\\1$", stringg) - TRE regex: (\.) captures a literal dot into a capturing group with ID 1 and then $ asserts the position at the end of string, and then the match (the dot) is replaced with the same value that is stored in Group 1 (see the \1 backreference) and then a $ is added.
  • sub("$(?<=\\.)", "$", stringg, perl=TRUE) - PCRE regex: matches the end of string with $ and then checks if there is a dot immediately to the left of the current location with the help of the (?<=\.) positive lookbehind and then the location is replaced with $, i.e. the $ is just appended to the end of string
  • str_replace(stringg, "\\.$", "\\0$") - ICU regex: here, it is almost the same example as with the TRE regex above, but there is no need capturing the whole consuming pattern (the dot) since the stringr functions support the zeroth backreference, the one that refers to the whole match value. So, the . at the end of string is matched with the \.$ pattern and \0$ replaces the match with itself, with ., and $ is appended.
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563