我想评论SQL脚本的所有行,除了那些包含字符串TABLE1(区分大小写)的行,如果可能的话 - 只使用一个ant任务(replaceregexp?)。 理想情况下,应忽略注释(以“ - ”开头)和空行,但如果不是,则不太重要。
例: 初始文件
CREATE TABLE TEST (TEST_ID VARCHAR(255) NOT NULL, TEST_NAME VARCHAR(255));
CREATE TABLE LICENSE (ID BIGINT NOT NULL GENERATED BY DEFAULT AS IDENTITY, PRIMARY KEY (ID));
delete from TEST2 where
ID = 'whatever'
;
delete from TEST3 where
ENV = 'whatelse'
;
UPDATE TEST1 SET VERSION = '1.0';
最终文件
-- CREATE TABLE TEST (TEST_ID VARCHAR(255) NOT NULL, TEST_NAME VARCHAR(255));
-- CREATE TABLE LICENSE (ID BIGINT NOT NULL GENERATED BY DEFAULT AS IDENTITY, PRIMARY KEY (ID));
-- delete from TEST2 where
-- ID = 'whatever'
-- ;
-- delete from TEST3 where
-- ENV = 'whatelse'
-- ;
UPDATE TEST1 SET VERSION = '1.0';
我找到的唯一解决方案是: 1.注释掉脚本的所有行,然后取消注释与字符串TEST1匹配的行:
<replaceregexp file="${sql.file}"
match="(.*)"
replace="-- \1"
byline="true"
/>
<replaceregexp file="${sql.file}"
match="^-- (.*TEST1)"
replace="\1"
byline="true"
/>
创建一个仅包含我要保留的行的新文件:
<copy file="${sql.file}" tofile="${sql.file.bak}">
<filterchain>
<linecontains>
<contains value="TEST1"/>
</linecontains>
</filterchain>
</copy>
我对两种解决方案都不满意: 解决方案#1。使用2个任务并两次更新同一个文件 解决方案#2。删除我们想要保留的其他行作为评论
如果有人得到了正确答案,我就接受了。
谢谢, 塞布丽娜
答案 0 :(得分:1)
一种解决方案可能是
<replaceregexp file="${sql.file}"
match="(^(?!$))(?!.*TEST1)"
replace="-- \1"
byline="true" />
关键是?!
是“负前瞻断言” - 它表示当?!
之后括号中的字符串不发现。
这是模式的explanation:
NODE EXPLANATION
--------------------------------------------------------------------------------
( group and capture to \1:
--------------------------------------------------------------------------------
^ the beginning of the string
--------------------------------------------------------------------------------
(?! look ahead to see if there is not:
--------------------------------------------------------------------------------
$ before an optional \n, and the end of
the string
--------------------------------------------------------------------------------
) end of look-ahead
--------------------------------------------------------------------------------
) end of \1
--------------------------------------------------------------------------------
(?! look ahead to see if there is not:
--------------------------------------------------------------------------------
.* any character except \n (0 or more times
(matching the most amount possible))
--------------------------------------------------------------------------------
TEST1 'TEST1'
--------------------------------------------------------------------------------
) end of look-ahead