我有一段文本,其中有两种类型的注释。一种是用
输入1:
如果打开了一个注释,无论是用
%
分隔的注释,另一种是以/*
开始并以*/
结束的注释。例如:输入1:
Sarah was going out. % Remember she usually doesn't go out % It was very cold.
期望输出1: Sarah was going out. It was very cold.
输入2: Sarah was going out. /* Remember she usually doesn't go out */ It was very cold.
期望输出2: 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.
期望输出3: 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.
期望输出4: Charles knocked on the door and a woman
opened it. */ Perhaps this should happen
in chapter 10 instead?
基本上,当遇到开头的注释标记时,应删除直到其相应的结束注释标记为止(即使这意味着删除另一种类型的注释标记)。如果打开了一个注释,无论是用
%
还是/*
,但从未关闭,就会假定该注释将继续到文本结束。但是,如果只存在这种类型的结束标记*/
(因为打开者在另一个注释中并因此被删除),则应将其保留在文本中。
查尔斯敲门,一个女人打开了门。*/也许这应该在第10章发生?%她看着他。-是的?她说。
。你似乎想要.replaceAll("%[^%]*%|/\\*[^*]*\\*+(?:[^/*][^*]*\\*+)*/","")
。不过,最后一个%
可能需要可选,如"%[^%]*%?|/\\*[^*]*\\*+(?:[^/*][^*]*\\*+)*/"
。 - Wiktor Stribiżew.replaceAll("%[^%]*%?|/\\*[^*]*(?:\\*(?!/)[^*]*)*(?:\\*/)?","")
。 - Wiktor Stribiżew