regex - 在 VI 编辑器中将每个出现的正则表达式匹配替换为特定的正则表达式?

标签 regex linux vim vi regular-language

假设我有一个如下的文本文件。

create table "kevin".tb1 {
col1,
col2
}
create table "jhone".tb2 {
col1,
col2
}
create table "jake".tb3 {
col1,
col2

}

我需要通过将出现的每个表所有者名称替换为名为“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\+"/\1 "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
\1 "informix" — replace what is found with backreference 1 (text "create table"), a space and text "informix"

关于regex - 在 VI 编辑器中将每个出现的正则表达式匹配替换为特定的正则表达式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53704308/

相关文章:

php - 获取方括号的内容,避免嵌套括号

python - 查找正则表达式中的最后一组

vim - 为什么这个vim功能不能工作但将其作为cmd运行

regex - 如何使用 Windows 批处理文件拆分文本文件元素?

linux - `gulp` 命令在 Linux 上无法正常工作

c++ - 为什么在运行时更改 LD_LIBRARY_PATH 不会在可执行文件加载后反射(reflect)在可执行文件上

linux - bash 中类似 pgrep 的高级进程搜索

vim - 在正常模式下,为什么 `q:` 会显示命令历史记录?

linux - 我如何去 vim 中的第 X 场比赛?

.net - 需要帮助构建正则表达式以匹配特定字符串(内部示例)