[英]java replace - replace text in string between two tags that are seperated by spaces and new line
String abc =
"<Message>
<Details> hello </Details>
</Message>
<Customer>
<Details> John </Details>
</Customer>
<Bank>
<Details> BANK1 <Details>
<Bank>"
Now I want replace string whatever there between "customer/Details"(here-John) with static text(ex: Peter), that is replace John with Peter. 现在,我想用静态文本(例如:Peter)替换“ customer / Details”(此处为John)之间的任何字符串,即用Peter替换John。 How to do this in java probably with ReplaceAll function + regular Expression.
如何在Java中执行此操作,可能需要使用ReplaceAll函数+正则表达式。 I dont want to disturb format of string which is in xml format after replacement.
我不想打扰替换后为xml格式的字符串格式。 Expected output
预期产量
String abc =
"<Message>
<Details> hello </Details>
</Message>
<Customer>
<Details> Peter</Details>
</Customer>
<Bank>
<Details> BANK1 <Details>
<Bank>"
You forgot the whitespace inside the tokens you are recognising: 您忘记了所识别的令牌内的空格:
abc.replaceAll("(<Customer>\\s*<Details>)[^<]+(</Details>\\s*</Customer>)", "$1 Fred $2");
The \\s
character class recognises all whitespace including \\n
. \\s
字符类可识别所有空格,包括\\n
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.