我收到了包含一些标记的文本。例如:
Jane and Jack <record>went</record> to <record>cinema</record>.
My objective is to convert this sentence to:
Jane and Jack {blank} to {blank}.
When I use the following
text.replaceAll("<record>.*</record>", "{blank}");
我收到“Jane 和 Jack {blank}”。而不是上面那句话。
解决这个问题的最佳方法是什么?
最佳答案
这应该可以做到:
text.replaceAll("<record>.*?</record>", "{blank}");
添加 ?
会使匹配“非贪婪”,因此它会匹配最少数量的元素,而不是最多的元素。
另请注意,处理这些类型的替换最好留给 XML 解析器,除非它们是简单的替换。
关于java - 使用 java regex 处理标记标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17243008/