代码之家  ›  专栏  ›  技术社区  ›  chappar

双引号括号内的字符串

  •  2
  • chappar  · 技术社区  · 17 年前

    我想把“(”和“)中的所有字符串都双引号引起来。

    i、 e fullname=(“此”为“测试”名称);

    谁能给我一个vim正则表达式来做这个?

    3 回复  |  直到 17 年前
        1
  •  4
  •   Fritz G. Mehner    17 年前

    我会这样做:

    :s/\<\w\+\>/"&"/gc
    

    由于确认开关“c”,每次更换都会要求您。

        2
  •  3
  •   Tomalak    17 年前

    这就是:

    :%s/\v(\(.*)@<=[[:alnum:]]+(.*\))@=/"&"/g
    

    内容如下:

    :%s               substitute on all lines
    /                 matching
    \v                (with "very magic" switched on)
    (\(.*)@<=         a position that follows an opening paren, on this line
    [[:alnum:]]+      a series of alphanumeric characters (i.e. "words")
    (.*\))@=          that are followed by a closing paren, on this line
    /                 replace with
    "&"               the match, in quotes
    /g                globally
    

    值得注意的是,vim实际上支持可变长度的后视。大多数现代正则表达式实现都没有。

        3
  •  0
  •   pts    17 年前

    此命令将空格、制表符、等号、括号或分号以外的所有内容置于引号中:

    :s/[^ \t=();][^ \t=();]*/"&"/g
    

    请注意,此引用 fullname

    有时,手动完成部分工作是值得的,因为自动化会更慢。

    运行以下命令:

    :help substi
    

    获取有关vim中regexp替换的帮助。