Regex 如何在Stata中每次出现字符串时向字符串添加递增值?

Regex 如何在Stata中每次出现字符串时向字符串添加递增值?,regex,stata,Regex,Stata,我有一个名为talk的字符串变量。假设我想在talk中找到单词“please”的所有实例,并在每一行中为每个包含单词递增计数的“please”添加后缀 例如,如果talk如下所示: "will you please come here please do it as soon as you can if you please" 我希望它看起来像这样: "will you please1 come here please2 do it as soon as you

我有一个名为
talk
的字符串变量。假设我想在
talk
中找到单词“please”的所有实例,并在每一行中为每个包含单词递增计数的“please”添加后缀

例如,如果
talk
如下所示:

"will you please come here please do it as soon as you can if you please"
我希望它看起来像这样:

"will you please1 come here please2 do it as soon as you can if you please3"
换句话说,“please1”表示它是第一个出现的“please”,而“please2”则是第二个,以此类推

我已经用正则表达式和几个循环编写了一些代码(如下),但它并不完美,即使我可以解决这些问题,它似乎也过于复杂有更简单的方法吗?

# I first extract the portion of 'talk' beginning from the 1st please to the last   
    gen talk_pl = strtrim(stritrim(regexs(0))) if regexm(talk, "please.+please")
# I count the number of times "please" occurs in 'talk_pl'
    egen count = noccur(talk_pl), string("please")
# in the loop below, x = 2nd to last word; i = 3rd to last word 
    qui levelsof count
    foreach n in `r(levels)' {
            local i = `n' -1
            local x = `i' -1
            replace talk_pl = regexrf(talk_pl, "please$", "please`n'") if count == `n'      
            replace talk_pl = regexrf(talk_pl, "please (?=.+?please`n')", "please`i' ") if count == `n' 
            replace talk_pl = regexrf(talk_pl, "please (?=.+?please`i')", "please`x' ") if count == `n'         
        }

不知道为什么我的帖子中的代码是彩色的!这比我想做的要简单得多-谢谢!!
* Example generated by -dataex-. To install: ssc install dataex
clear
input str71 talk
"will you please come here please do it as soon as you can if you please"
end

// Install egenmore if not installed already
* ssc install egenmore

clonevar wanted = talk

// count occurrences of "please"
egen countplease = noccur(talk), string(please)

// Loop over 1 to max number of occurrences
sum countplease, meanonly 
forval i = 1/`r(max)' {
    replace wanted = ustrregexrf(wanted, "\bplease\b", "please`i'")
}
list

     +---------------------------------------------------------------------------------------+
  1. |                                                                           talk        |
     |        will you please come here please do it as soon as you can if you please        |
     |---------------------------------------------------------------------------------------|
     |                                                                     wanted | countp~e |
     | will you please1 come here please2 do it as soon as you can if you please3 |        3 |
     +---------------------------------------------------------------------------------------+