[英]Replace a String between two Strings and Boundary words also
I want to replace string variable text between two words and replace the boundary words themselves.我想替换两个单词之间的字符串变量文本并替换边界词本身。 Similar to this question, however I want to replace between &firstString
and &endString
with newText
.与这个问题类似,但是我想用newText
替换&firstString
和&endString
。
Replace a String between two Strings 在两个字符串之间替换一个字符串
Input:输入:
abcd&firstString={variableText}&endStringxyz
Output: Output:
abcdnewTextxyz
I could just do two str.replaceAll(&firstString)
and str.replaceAll(&secondString)
.我可以做两个str.replaceAll(&firstString)
和str.replaceAll(&secondString)
。 However, is it possible to do in one line of code changing maybe this code solution?但是,是否可以在一行代码中更改此代码解决方案?
String newstr = str.replaceAll("(&firstString=)[^&]*(&endString=)", "$1foo$2");
The question is really confusing but I think I have got it based on the other answer.这个问题真的很令人困惑,但我想我是根据其他答案得到的。
You want to replace &firstString={XYZ}&endString
with the value of a XYZ
String variable?您想将&firstString={XYZ}&endString
替换为XYZ
字符串变量的值吗?
First of all, in Java, one cannot reference a variable using a String that stores the variable name (Java is statically typed).首先,在 Java 中,不能使用存储变量名称的字符串来引用变量(Java 是静态类型的)。 To counteract this, you can have a HashMap that stores the 'variable' names as the keys and the replacement String as the value.为了解决这个问题,您可以使用 HashMap 将“变量”名称存储为键,并将替换字符串存储为值。
So, using your example we have:因此,使用您的示例,我们有:
String in = "abcd&firstString={variableText}&endStringxyz";
and you want to replace variableText
with "newText"
.并且您想将variableText
替换为"newText"
。
So, we can do this:所以,我们可以这样做:
Map<String, String> replacementMap = new HashMap<>(){{
put("variableText", "newText"); // replace "variableText" with "newText"
put("foo", "bar"); // replace "foo" with "bar"
}};
We can then declare a utility method for replacement (slightly modified code gotten from here ):然后我们可以声明一个用于替换的实用方法(从这里获得的稍微修改的代码):
private static final Pattern replaceTextPattern = Pattern.compile("&firstString=\\{.*?\\}&endString");
public static String replaceText(String input, Map<String, String> replacementMap){
StringBuilder sb = new StringBuilder();
Matcher matcher = replaceTextPattern.matcher(input);
while(matcher.find()){
String match = matcher.group();
String key = match.substring(
match.indexOf('{')+1,
match.lastIndexOf('}')
);
String replacement = replacementMap.get(key);
matcher.appendReplacement(sb, replacement);
}
matcher.appendTail(sb);
return sb.toString();
}
And finally call the method:最后调用方法:
System.out.println(replaceText(in, replacementMap)); // abcdnewTextxyz
It even works with multiple variables:它甚至适用于多个变量:
System.out.println(replaceText("abcd&firstString={variableText}&endStringxyz&firstString={foo}&endStringdef", replacementMap)); // abcdnewTextxyzbardef
String replacement = "newText";
String text = "abcd&firstString={currentText}&secondStringxyz";
String result = text.replaceAll("&firstString=\\{.*?\\}&secondString",replacement);
System.out.println(result);
prints印刷
abcdnewTextxyz
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.