douli2063 2011-04-11 09:09
浏览 42
已采纳

不要在vim中替换内部的php-strings

At my company we have the code convention to write $foo . $bar (with whitespaces). Because I couldn't get familiar with that (I don't want to discuss :P ), I wrote the following vim-replace-function:

:%s/\(\w\|\]\|'\|\"\)\s\?\.\s\?\('\|\$\|\"\|(\)/\1 \. \2/eg

..which matches everything I needed so far. But now I also have String which I don't want to replace.

So how can I ignore Strings like "foo.bar" or 'foo.bar', but not $foo.$bar ?

Update: I would also be happy with a completely scripted function. I just wonder if there is no other way than temporarily remove all php-strings. Re-calculating the actual replacement-positions would be a mess, no?

  • 写回答

1条回答 默认 最新

  • doushun9875 2011-06-08 18:50
    关注

    You can make use of syntax highlighting here:

    :%s/\(\w\|\]\|'\|\"\)\s\?\.\s\?\('\|\$\|\"\|(\)/\=synIDattr(synID(line('.'),col('.'), 0),'name')!~?'phpString\(Single\|Double\)'?submatch(1) . ' . ' . submatch(2):submatch(1).'.'.submatch(2)/eg
    

    Basically that means, if the string below the cursor is not of Syntax type phpStringSingle and not phpStringDouble, then replace by adding a ' . ' between each capturing group, else leave it as it was.

    Read more about it at:

    :h sub-replace-expression
    :h expr1
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 TLS1.2协议通信解密
  • ¥40 图书信息管理系统程序编写
  • ¥20 Qcustomplot缩小曲线形状问题
  • ¥15 企业资源规划ERP沙盘模拟
  • ¥15 树莓派控制机械臂传输命令报错,显示摄像头不存在
  • ¥15 前端echarts坐标轴问题
  • ¥15 ad5933的I2C
  • ¥15 请问RTX4060的笔记本电脑可以训练yolov5模型吗?
  • ¥15 数学建模求思路及代码
  • ¥50 silvaco GaN HEMT有栅极场板的击穿电压仿真问题