知道java中怎么用正则表达式去掉sql代码段的注释吗?
比如
create teable testtable1as
(select * from tablename1
);
--sql2
create teable testtable2as
(select * from tablename2
);
/*sql3*/
create teable testtable3as
(select * from tablename3
);
要把这两种注释都去了,正则表达式如何写呀?
---------------------------------------------------------------
表示/* ... */的表达式:
"[/*].*((/r)?+(/n)*(.)*)*[*/]"
---------------------------------------------------------------
// 测试代码
String regex = "[/*].*((/r)?+(/n)*(.)*)*[*/]";
String[] sa = new String[5];
// 格式1
/**
* ABCDEFG
* ABCDEFAG
* DGDGDG
*/
sa[0] = "/**/r/n * ABCDEFG/r/n * ABCDEFAG/r/n * DGDGDG/r/n */"; // /r/n
sa[1] = "/**/n * ABCDEFG/n * ABCDEFAG/n * DGDGDG/n */"; // /n
// 格式2
/* ABCDEFG
ABCDEFAG
DGDGDG */
sa[2] = "/*ABCDEFG/r/nABCDEFAG/r/nDGDGDG*/"; // /r/n
sa[3] = "/*ABCDEFG/nABCDEFAG/nDGDGDG*/"; // /n
// 格式3
/*ABCDEFGABCDEFAGDGDGDG*/
sa[4] = "/*ABCDEFGABCDEFAGDGDGDG*/";
for (int i=0; i<sa.length; i++) {
System.out.println(
Pattern.matches(regex, sa[i]));
}
匹配注释的正则表达式
最新推荐文章于 2023-12-10 02:38:49 发布