Reputation: 101
I am trying to replace strings which start with a number inside a square bracket with a word.
[1]FirstWord!FF >> FirstReplace!!FF
[2]SecondWord!FF >> SecondReplace!!FF
This code only works if there is no [number]
at the beginning.
'test!FF' %>% str_replace_all("test(['\"])?!","replace!")
But how can I update the pattern if I have something like below:
'[1]test!FF' %>% str_replace_all("[1]test(['\"])?!","replace!")
Upvotes: 2
Views: 709
Reputation: 626929
You want
'[1]test!FF' %>% str_replace_all("\\[\\d+]test['\"]?!", "replace!")
## => [1] "replace!FF"
Note: If the [number]
part is optional, wrap it with an optional non-capturing group: "(?:\\[\\d+])?test['\"]?!"
.
Details:
\[
- a [
char\d+
- one or more digits]
- a ]
chartest
- a test
word['\"]?
- an optional "
or '
!
- a !
char.Upvotes: 1