質問

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