【问题标题】:Replace every occurrence of the regular expression matching with a particular in VI editor?用VI编辑器中的特定匹配替换每一个匹配的正则表达式?
【发布时间】:2019-05-11 06:32:59
【问题描述】:

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

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

我可以单独更换它们,但我需要一次全部更换。

【问题讨论】:

  • 到目前为止你尝试了什么?
  • 我修改了。但我需要使用正则表达式来过滤这些单词
  • (?<=table ")(.*?)(?=") 试试这个正则表达式并替换'informix'
  • 它不工作
  • 在这里查看演示 regex101.com/r/8fsO2Y/2

标签: regex linux vim vi regular-language


【解决方案1】:
%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"

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-05-16
    • 2012-09-28
    • 1970-01-01
    • 2016-11-04
    • 1970-01-01
    • 1970-01-01
    • 2011-06-17
    相关资源
    最近更新 更多