英文:
How to replace an unknown-substring in a string with specific pattern using Java code?
问题
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class StringReplacement {
public static void main(String[] args) {
String string1 = "text1 src=\"arbitrary_string_1\" text2";
String string2 = "text3 src=\"arbitrary_string_2\" text4";
String replacement = "stackoverflow";
String updatedString1 = replaceArbitraryString(string1, replacement);
String updatedString2 = replaceArbitraryString(string2, replacement);
System.out.println(updatedString1);
System.out.println(updatedString2);
}
public static String replaceArbitraryString(String input, String replacement) {
String pattern = "src=\"[^\"]+\"";
Pattern regexPattern = Pattern.compile(pattern);
Matcher matcher = regexPattern.matcher(input);
if (matcher.find()) {
String found = matcher.group();
return input.replace(found, "src=\"" + replacement + "\"");
}
return input;
}
}
这段代码可以用来替换类似于 "string1 src="arbitrary string" string2"
格式的字符串中的 "arbitrary string"
部分为特定的字符串(例如 "stackoverflow"
)。
英文:
I deal with many strings whose pattern is similar "string1 src="arbitrary string" string2"
and I want to replace the "arbitrary string"
with specific string (let's say "stackoverflow"
)
using Java code.
For example, change these strings:
String string1 = "text1 src="arbitrary_string_1" text2"
String string2 = "text3 src="arbitrary_string_2" text4"
to:
string1 = "text1 src="stackoverflow" text2"
string2 = "text3 src="stackoverflow" text4"
I tried several codes using substring() function like:
String string1 = "text1 src=\"arbitrary string\" text2" \\declaration of string1
Sting newString = string1.substring(0,indexOf("src")) + "src=\"stackoverflow\"" \\trying of the replacement
but it clearly cuts the ending of the string "code".
I also thought about the option of replacement using the pattern of the strings I deal with, but I dont know how to do that..
Any help would be greatly appreciated!
答案1
得分: 0
看一下这个:
public static String replaceAbitrary(String input, String replacement) {
// 从开头到第一个 " 的部分。如果你不想在结果中包含 ",可以移除 +1
String start = input.substring(0, input.indexOf('\"') + 1);
// 从最后一个 " 开始的部分。如果你不想在结果中包含 ",添加一个 +1
String end = input.substring(input.lastIndexOf('\"'));
// 把它们组合起来
return start + replacement + end;
}
public static void main(String[] args) throws IOException {
// 输入部分。注意,这个系统甚至允许任意字符串中包含引号。
String input = "text1 src=\"arbitrary \"string\"\" text2";
// 要替换成的字符串。
String replacement = "stackoverflow";
System.out.println(replaceAbitrary(input, replacement));
}
这会输出 text1 src="stackoverflow" text2
。如果你不想包含引号,按照代码内的指示操作。
英文:
Take a look at this:
public static String replaceAbitrary(String input, String replacement) {
// The input up to the first ". If you don't want to include the " in you
// result, remove the +1
String start = input.substring(0, input.indexOf('"') + 1);
// The input starting from the last ". If you don't want to include the ", add a
// +1
String end = input.substring(input.lastIndexOf('"'));
// Put it all together
return start + replacement + end;
}
public static void main(String[] args) throws IOException {
// The input. Note that this system even allows quotation marks inside the
// arbitrary string.
String input = "text1 src=\"arbitrary \"string\"\" text2";
// The string to put instead of the arbitrary one.
String replacement = "stackoverflow";
System.out.println(replaceAbitrary(input, replacement));
}
This outputs text1 src="stackoverflow" text2
. If you don't want the quotation marks included, follow the instructions inside the code.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论