匹配n次后使用regex backreference
我需要在木偶定义中匹配多个字符串:匹配n次后使用regex backreference,regex,atom-editor,regex-group,Regex,Atom Editor,Regex Group,我需要在木偶定义中匹配多个字符串: sysctl::conf { 'net.ipv4.neigh.default.gc_thresh1': value => '4096' } sysctl::conf { 'net.ipv4.neigh.default.gc_thresh2': value => '8192' } sysctl::conf { 'net.ipv4.neigh.default.gc_thresh3': value => '16384' } s
sysctl::conf { 'net.ipv4.neigh.default.gc_thresh1': value => '4096' }
sysctl::conf { 'net.ipv4.neigh.default.gc_thresh2': value => '8192' }
sysctl::conf { 'net.ipv4.neigh.default.gc_thresh3': value => '16384' }
sysctl::conf { 'net.ipv6.conf.all.disable_ipv6': value => '0' }
sysctl::conf { 'net.ipv6.conf.default.disable_ipv6': value => '0' }
然后使用backreference添加相同的行,再添加一行,如下所示:
sysctl::conf { 'net.ipv4.neigh.default.gc_thresh1': value => '4096' }
sysctl::conf { 'net.ipv4.neigh.default.gc_thresh2': value => '8192' }
sysctl::conf { 'net.ipv4.neigh.default.gc_thresh3': value => '16384' }
sysctl::conf { 'net.ipv6.conf.all.disable_ipv6': value => '0' }
sysctl::conf { 'net.ipv6.conf.default.disable_ipv6': value => '0' }
sysctl::conf { 'net.ipv4.tcp_mtu_probing': value => '1' }
我尝试了这个匹配模式(sysctl::conf\{.*\}\n{1,})
,然后将其替换为\1\n sysctl::conf{'net.ipv4.tcp\u mtu}:value=>'1'}
问题是,反向引用引用的是每一行,而不是所有分组块,以便在末尾添加新行。这是我得到的结果,而不是期望的结果:
sysctl::conf { 'net.ipv4.neigh.default.gc_thresh1': value => '4096' }
sysctl::conf { 'net.ipv4.tcp_mtu_probing': value => '1' }
sysctl::conf { 'net.ipv4.neigh.default.gc_thresh2': value => '8192' }
sysctl::conf { 'net.ipv4.tcp_mtu_probing': value => '1' }
sysctl::conf { 'net.ipv4.neigh.default.gc_thresh3': value => '16384' }
sysctl::conf { 'net.ipv4.tcp_mtu_probing': value => '1' }
sysctl::conf { 'net.ipv6.conf.all.disable_ipv6': value => '0' }
sysctl::conf { 'net.ipv4.tcp_mtu_probing': value => '1' }
sysctl::conf { 'net.ipv6.conf.default.disable_ipv6': value => '0' }
sysctl::conf { 'net.ipv4.tcp_mtu_probing': value => '1' }
我是否应该尝试将整个线条块作为一个模式匹配,而不是同一模式的多个匹配
我这样做是因为我使用Atom搜索和替换多个不同文件中的所有sysctl::conf
条目,并在已经存在的条目之后添加新条目
谢谢:)
PS:已经知道atom中的反向引用称为$1而不是\1。您可以尝试使用此模式
(?:\s*sysctl::conf \{.*\})+
之后,替换为
\0\n sysctl::conf { 'net.ipv4.tcp_mtu_probing': value => '1' }
注意\0
,因为没有任何捕获组
说明:
是用于捕获孔块的非捕获组(即?:
)\0
匹配任何空白字符(等于[\r\n\t\f\v])\s*
以捕获所有匹配项+
:%s/\%(\s*sysctl::conf{.*}\n\)\+/\0 sysctl::conf{'net.ipv4.tcp_mtu探测]:value=>'1'}/g
。请让我知道它是否有效。太好了!很好!非常感谢你,米格尔:)很高兴我能帮忙:)