我正在使用 vscode vimplugin。我有一堆看起來像的行:
Terry,169,80,,,47,,,22,,,6,,
我想洗掉第一個逗號后的所有字母數字字符,所以我得到:
Terry,,,,,,,,,,,,,
在命令模式下,我嘗試過:
s/^. \,[a-zA-Z0-9-]\ //g
但這似乎沒有任何作用。我怎樣才能得到這個作業?
編輯:
s/^[^,]\ ,[a-zA-Z0-9-]\ //g
uj5u.com熱心網友回復:
\ 很貪心;^.\ ,吃到最后一 ,排。
而不是點(表示“任何字符”)使用[^,]表示“除逗號之外的任何字符”。然后表示“直到第一個逗號^[^,]\ ,的任何字符”。
您的要求的問題是您想在開始使用錨定,^因此您不能使用標志g- 使用錨定任何替換都將完成一次。我可以解決這個難題的唯一方法是使用運算式:匹配并保留錨定文本,然后使用substitute()帶有 flag的函式g。
我用以下運算式進行了管理:
:s/\(^[^,]\ \)\(,\ \)\(.\ \)$/\=submatch(1) . submatch(2) . substitute(submatch(3), '[^,]', '', 'g')/
讓我把它分成幾部分。搜索:
\(^[^,]\ \) — first, match any non-commas
\(,\ \) — any number of commas
\(.\ \)$ — all chars to the end of the string
替代:
\= — the substitution is an expression
見http://vimdoc.sourceforge.net/htmldoc/change.html#sub-replace-expression
submatch(1) — replace with the first match (non-commas anchored with ^)
submatch(2) — replace with the second match (commas)
substitute(submatch(3), '[^,]', '', 'g') — replace in the rest of the string
最后一個呼叫substitute()很簡單,它將所有非逗號替換為空字串。
PS。真實測驗vim,而不是 vscode。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/459389.html
標籤:视觉工作室代码
