A particular string can contain multiple instances of a pattern that I'm trying to match. For example, if my pattern is <N(.+?)N>
and my string is "My name is <N Timon N> and his name is <N Pumba N>"
, then there are two matches. I want to replace each match with a replacement that includes an index for which match is being replaced.
So in my string "My name is <N Timon N> and his name is <N Pumba N>"
,
I want to change the string to read "My name is [Name #1] and his name is [Name #2]"
.
How do I accomplish this, preferably with a single function? And preferably using functions from stringr
or stringi
?