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?

Était-ce utile?

La 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.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top