如何在流式传输 SQL 文件时忽略多行注释

问题描述 投票:0回答:1

我正在尝试使用 Java 流 API 迭代 SQL 文件。我想忽略 SQL 文件中的注释。

我可以处理一行写的评论。

例如:

/* Hello World */
if ((line.trim().startsWith("/*") & line.trim().endsWith("*/")) ) {
    return;
}

但是如何处理多行编写的注释,例如:

/*
Hello

World

 */

do while 循环内可以写什么?

if (line.trim().startsWith("/*")){
      do {
          //How to iterate through the stream here to find the ending of the comment.
              }
    while ( ! line.trim().endsWith("*/"));
java java-stream
1个回答
0
投票

如果您可以自由地将文件读取为

String
那么您可以使用正则表达式来过滤掉注释行:

String linesWithoutComments = data.replaceAll("(?s)/\\*.+?\\*/", "");

测试数据:

/* Hello

World

*/

This is line 1

This is line 2

/* Some comment */

This is line 3

输出:



This is line 1

This is line 2



This is line 3
© www.soinside.com 2019 - 2024. All rights reserved.