简体   繁体   English

替换所有未引用的事件

[英]replace every occurrence that is unquoted

I'm trying to write a regex in Vim to replace every occurrence of true with 't' and false with 'f' in an SQL file. 我试图在Vim中编写一个正则表达式,以在SQL文件中将每次出现的true替换为't' ,将false替换为'f' To complicate this, I shouldn't replace true or false when they occur anywhere inside quotes. 复杂的是,当引号内的任意位置出现truefalse时,我不应该替换它们。

I'm using the following regex which almost works: 我正在使用几乎可以正常工作的以下正则表达式:

%s/\v^(([^'"]*)|(.*('.*'|".*").*)*)\zs((t)rue|(f)alse)/'\6\7'/
#       no qts or even number of qts   \6     \7

But with one small problem: It only replaces one set of true s and false s per run. 但是有一个小问题:每次运行仅替换一组truefalse Eg (matches highlighted in bold): 例如(匹配项以粗体突出显示):

INSERT INTO pages VALUES (42, NULL, 'string', true, '', NULL, true, false , 1); INSERT INTO pages VALUES (42, NULL, 'string', true, NULL, true, false , 1); INSERT INTO pages VALUES (42, NULL, true , '', NULL, true, false, 1); INSERT INTO pages VALUES (42, NULL, true, NULL, true, false , 1);

I've tried all sorts of stuff, and I've searched the internet but can't find anything, so I'm stuck. 我已经尝试过各种方法,并且已经在互联网上搜索但找不到任何东西,所以我陷入了困境。 Are there any regex wizards here who know how I can fix this? 这里有任何正则表达式向导谁知道我该如何解决?

A Vim or Ruby solution would be ideal, but a general solution would be helpful as well. Vim或Ruby解决方案将是理想的选择,但一般的解决方案也将有所帮助。

FYI: This is for a rake task that pulls the remote PG database into a local SQLite3 database. 仅供参考:这是一个瑞克任务,它将远程PG数据库拉入本地SQLite3数据库。

Using Vim substitute along with negative look-behinds and negative look-aheads, along with taking some inspiration from your current attempt: 使用Vim substitute以及否定的后瞻性和否定的前瞻性,以及从当前尝试中获得的一些启发:

%s/\([\S"']\)\@<!\(\(t\)rue\|\(\(f\)alse\)\)\([\S"']\)\@!/'\3\5'/g

The following 下列

INSERT INTO pages VALUES (42, NULL, 'string true', true, '', NULL, true, false, 1);
INSERT INTO pages VALUES (42, NULL, "string false", true,     NULL, true, false, 1);
INSERT INTO pages VALUES (42, NULL,           true, '', NULL, true, false, 1);
INSERT INTO pages VALUES (42, NULL,           true,     NULL, true, false, 1);

Turns into: 变成:

INSERT INTO pages VALUES (42, NULL, 'string true', 't', '', NULL, 't', 'f', 1);
INSERT INTO pages VALUES (42, NULL, "string false", 't',     NULL, 't', 'f', 1);
INSERT INTO pages VALUES (42, NULL,           't', '', NULL, 't', 'f', 1);
INSERT INTO pages VALUES (42, NULL,           't',     NULL, 't', 'f', 1);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM