Regex 使用正则表达式或subinstr()清除本地宏

Regex 使用正则表达式或subinstr()清除本地宏,regex,stata,local,stata-macros,Regex,Stata,Local,Stata Macros,我的目标是从\uuu和单词末尾下划线后面的所有数字中清除给定的局部。假设我在单词的末尾只有下划线和数字 通过使用subinstr() local list_x `" "rep78_3" "make_1" "price_1" "mpg_2" "' local n_x : list sizeof list_x forvalues j = 1/`n_x' { local varname: word `j' of `list_x' local clean_name: subinstr

我的目标是从
\uuu
和单词末尾下划线后面的所有数字中清除给定的局部。假设我在单词的末尾只有下划线和数字

通过使用
subinstr()

local list_x `" "rep78_3" "make_1" "price_1" "mpg_2" "'
local n_x : list sizeof list_x

forvalues j = 1/`n_x' {
    local varname: word `j' of `list_x'
    local clean_name: subinstr local varname "_1" "" 
    display "`clean_name'" 
}
我试图查看
regexm()
regexs()
,但我不太确定如何设置代码

我知道可能有多种方法可以解决这个问题


也许有一种更简单的方法来解决我看不到的问题?

使用字符串函数:

local list_x rep78_3 make_1 price_1 mpg_2

// assumes only one _
foreach elem of local list_x {
    local pos = strpos("`elem'", "_")
    local clean = substr("`elem'", 1, `pos' - 1) 
    di "`clean'" 
}

// considers last _ (there can be multiple)
foreach elem of local list_x {
    local pos = strpos(reverse("`elem'"), "_")
    local clean = reverse(substr(reverse("`elem'"), `pos' + 1, .))
    di "`clean'" 
}
如果您喜欢,可以嵌套函数调用。请参阅帮助字符串函数


正则表达式也应该起作用。

使用字符串函数:

local list_x rep78_3 make_1 price_1 mpg_2

// assumes only one _
foreach elem of local list_x {
    local pos = strpos("`elem'", "_")
    local clean = substr("`elem'", 1, `pos' - 1) 
    di "`clean'" 
}

// considers last _ (there can be multiple)
foreach elem of local list_x {
    local pos = strpos(reverse("`elem'"), "_")
    local clean = reverse(substr(reverse("`elem'"), `pos' + 1, .))
    di "`clean'" 
}
如果您喜欢,可以嵌套函数调用。请参阅帮助字符串函数


正则表达式也应该起作用。

使用正则表达式,解决方案是:

local list_x `" "rep78_3" "make_1" "price_1" "mpg_2" "'
local n_x : list sizeof list_x

forval j = 1/`n_x' {
    local varname: word `j' of `list_x'
    local clean_name = regexr("`varname'" , "_[0-9]$" , "")
    di "`clean_name'" 
}

使用正则表达式,解决方案是:

local list_x `" "rep78_3" "make_1" "price_1" "mpg_2" "'
local n_x : list sizeof list_x

forval j = 1/`n_x' {
    local varname: word `j' of `list_x'
    local clean_name = regexr("`varname'" , "_[0-9]$" , "")
    di "`clean_name'" 
}

使用Stata 14中的新版本正则表达式函数,您可以一次替换所有匹配项

. local list_x `" "rep78_3" "make_1" "price_1" "mpg_2" "'

. local fixed = ustrregexra(`"`list_x'"', "_[0-9]+","")

. dis `"`fixed'"'
 "rep78" "make" "price" "mpg" 

使用Stata 14中的新版本正则表达式函数,您可以一次替换所有匹配项

. local list_x `" "rep78_3" "make_1" "price_1" "mpg_2" "'

. local fixed = ustrregexra(`"`list_x'"', "_[0-9]+","")

. dis `"`fixed'"'
 "rep78" "make" "price" "mpg" 

通过组合
subinstr()
函数和
confirm
命令,也可以执行相同的操作:

local list_x rep78_3 make_1 price_1 mpg_2

local new_list_x = subinstr("`list_x'", "_", " ", .)

foreach x of local new_list_x {
    capture confirm number `x'
    if _rc != 0 {
        local final_list_x `final_list_x' `x'
    }
}

display "`final_list_x'"
rep78 make price mpg

通过组合
subinstr()
函数和
confirm
命令,也可以执行相同的操作:

local list_x rep78_3 make_1 price_1 mpg_2

local new_list_x = subinstr("`list_x'", "_", " ", .)

foreach x of local new_list_x {
    capture confirm number `x'
    if _rc != 0 {
        local final_list_x `final_list_x' `x'
    }
}

display "`final_list_x'"
rep78 make price mpg

谢谢,这是有效的,我还发布了如何通过使用正则表达式来解决这个问题的完整性。谢谢,这是有效的,我还发布了如何通过使用正则表达式来解决这个问题的完整性。