0

_私の目的は、特定のローカルの fromと、単語の末尾にあるアンダースコアに続くすべての数字をきれいにすることです。単語の末尾のみにアンダースコアとそれに続く数字があるとします。

を使用subinstr()することで、削除したいことを指定できます_1(そして、おそらく別の数値をループします)。

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()たが、コードの設定方法がよくわかりません。

これを解決するには複数の方法があるかもしれないことを理解しています。

見えない問題に対処する簡単な方法があるのではないでしょうか?

4

4 に答える 4

1

文字列関数の使用:

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'" 
}

それがあなたの好みであれば、関数呼び出しをネストできます。を参照してくださいhelp string functions

正規表現も機能するはずです。

于 2016-03-16T16:12:23.190 に答える
0

正規表現を使用すると、解決策は次のとおりです。

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'" 
}
于 2016-03-16T16:17:11.167 に答える
0

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
于 2018-06-08T14:47:16.337 に答える