Question

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?

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top