在 VI 编辑器中用特定的正则表达式匹配替换每个匹配项?
Replace every occurrence of the regular expression matching with a particular in VI editor?
假设我有一个文本文件如下。
create table "kevin".tb1 {
col1,
col2
}
create table "jhone".tb2 {
col1,
col2
}
create table "jake".tb3 {
col1,
col2
}
我需要按如下方式获取该文本文件,方法是将出现的每个 table 所有者名称替换为名为 "informix" 的相同名称。
输出应该像
create table "informix".tb1 {
col1,
col2
}
create table "informix".tb2 {
col1,
col2
}
create table "informix".tb3 {
col1,
col2
}
在 vi 编辑器中,
:%s/"kevin"/"informix"/g
我可以单独更换它们,但我需要一次全部更换。
%s/\(create table\) "\i\+"/ "informix"/
解释:
% — run through every line in the file
s/ — search and replace
\(create table\) — match the text and store it in the backreference 1
"\i\+" — match any number (more than 1) of identifier characters inside double quotes
"informix" — replace what is found with backreference 1 (text "create table"), a space and text "informix"
假设我有一个文本文件如下。
create table "kevin".tb1 {
col1,
col2
}
create table "jhone".tb2 {
col1,
col2
}
create table "jake".tb3 {
col1,
col2
}
我需要按如下方式获取该文本文件,方法是将出现的每个 table 所有者名称替换为名为 "informix" 的相同名称。
输出应该像
create table "informix".tb1 {
col1,
col2
}
create table "informix".tb2 {
col1,
col2
}
create table "informix".tb3 {
col1,
col2
}
在 vi 编辑器中,
:%s/"kevin"/"informix"/g
我可以单独更换它们,但我需要一次全部更换。
%s/\(create table\) "\i\+"/ "informix"/
解释:
% — run through every line in the file
s/ — search and replace
\(create table\) — match the text and store it in the backreference 1
"\i\+" — match any number (more than 1) of identifier characters inside double quotes
"informix" — replace what is found with backreference 1 (text "create table"), a space and text "informix"