2017-07-25 82 views
-1

我的字符串如下,我需要在输出中替换ref的值。从xml字符串中替换字符串的Java正则表达式

Input String: 
<sheet type="xl"><dimension ref="A1:W101"/></sheet> 

Output String: 
<sheet type="xl"><dimension ref="A1:j202"/></sheet> 

我已经尝试使用该(?< = \ B(REF =))。*(?= />),但它被匹配直到字符串的结尾。

任何人都可以帮助我,我如何使用正则表达式来替换ref值,如输出字符串。

+1

'*(=/>)' –

+0

使用XML解析器 – ChristofferPass

+0

是输入参数始终相同吗? – Lino

回答

1

您可以使用正则表达式是这样的:

String str = "<sheet type=\"xl\"><dimension ref=\"A1:W101\"/></sheet>"; 
String replaceBy = "A1:j202"; 

str = str.replaceAll("(ref=\")(.*)(\")", "$1" + replaceBy + "$3"); 
System.out.println(str); 

这也将工作:(?<= \ B(REF =))。?

str = str.replaceAll("(?<=\\b(ref=)).*?(?=/>)", "\"" + replaceBy + "\"");