I have received a text that contains some markup tags. For example:

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}");

I receive "Jane and Jack {blank}." instead of the sentence above.

What is the best way to approach this problem?

有帮助吗?

解决方案

This should do it:

text.replaceAll("<record>.*?</record>", "{blank}");

Adding the ? makes the match "non greedy" so it matches the fewest number of elements instead of the most.

Also note that handling these types of replacements are best left to an XML parser, unless they are simple replacements.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top