I am getting frustrated about how to correctly add an optional "final" full stop / period / dot to a string in a regex. I am looking to find a string with an optional dot. If there is anything else, this should not be returned.
I tried approaches suggested in
- Optional dot in regex
- Regex how to match an optional character
- Ruby Regular Expression If String has optional period, confirm only 0-2 digits follow
x <- c("string", "string.", "string. and other")
grepl("string(?=\\.)", x, perl = TRUE)
#> [1] FALSE TRUE TRUE
grepl("string(\\.?)", x, perl = TRUE)
#> [1] TRUE TRUE TRUE
grepl("string(\\.)?", x, perl = TRUE)
#> [1] TRUE TRUE TRUE
grepl("string(\\.$)?", x, perl = TRUE)
#> [1] TRUE TRUE TRUE
#desired output
c(TRUE, TRUE, FALSE)
#> [1] TRUE TRUE FALSE
Created on 2021-03-28 by the reprex package (v1.0.0)