【发布时间】:2020-06-07 09:11:54
【问题描述】:
我的文本包含两种类型的 cmets。由% 分隔的那些以及以/* 开头并以*/ 结尾的那些。例如:
输入1:
Sarah was going out. % Remember she usually doesn't go out % It was very cold.DESIRED_OUTPUT1:
Sarah was going out. It was very cold.输入2:
Sarah was going out. /* Remember she usually doesn't go out */ It was very cold.DESIRED_OUTPUT2:
Sarah was going out. It was very cold.输入3:
Charles knocked on the door and a woman opened it. % Hmm, is this good... /* Not sure */ Perhaps this should happen in chapter 10 instead? % She looked at him. - Yes?, she said.DESIRED_OUTPUT3:
Charles knocked on the door and a woman opened it. She looked at him. - Yes?, she said.输入4:
Charles knocked on the door and a woman opened it. % Hmm, is this good... /* Not sure to 100% */ Perhaps this should happen in chapter 10 instead? % She looked at him. - Yes?, she said.DESIRED_OUTPUT4:
Charles knocked on the door and a woman opened it. */ Perhaps this should happen in chapter 10 instead?
基本上,我希望在遇到开始注释标记时,所有内容都被删除,直到其各自的结束注释标记(即使这意味着删除其他类型的注释标记)。
如果使用% 或/* 打开评论,但从未关闭,则假定评论将持续到文本结尾。但是,如果它只是出现这种*/ 的结束标记(因为开启者在另一个评论中,因此被删除),它应该留在文本中。
【问题讨论】:
-
我认为输入 4 的结果应该是
Charles knocked on the door and a woman opened it. */ Perhaps this should happen in chapter 10 instead? % She looked at him. - Yes?, she said.。你似乎想要.replaceAll("%[^%]*%|/\\*[^*]*\\*+(?:[^/*][^*]*\\*+)*/","")。不过,您可能需要将最后一个%设为可选"%[^%]*%?|/\\*[^*]*\\*+(?:[^/*][^*]*\\*+)*/"。 -
嗯,感谢您指出这一点。我忘了提到是否没有结束标记(或者只是一个结束标记,因为开口在另一个评论中,因此被删除,就像在这种情况下一样)标记应该留在那里。所以输入/输出4是正确的。
-
所以,acc。根据您所说的(“标记应该留在那里”),不应在输入 4 中删除
% She looked at him. - Yes?, she said.。 -
嗯,你又是对的。如果使用 % 或 / * 打开评论,但从未关闭,则假定评论将持续到文本结尾。
-
啊哈,所以你可能想要
.replaceAll("%[^%]*%?|/\\*[^*]*(?:\\*(?!/)[^*]*)*(?:\\*/)?","")