如何使用Java代码将字符串中的未知子字符串替换为特定模式?

huangapple go评论94阅读模式
英文:

How to replace an unknown-substring in a string with specific pattern using Java code?

问题

  1. import java.util.regex.Matcher;
  2. import java.util.regex.Pattern;
  3. public class StringReplacement {
  4. public static void main(String[] args) {
  5. String string1 = "text1 src=\"arbitrary_string_1\" text2";
  6. String string2 = "text3 src=\"arbitrary_string_2\" text4";
  7. String replacement = "stackoverflow";
  8. String updatedString1 = replaceArbitraryString(string1, replacement);
  9. String updatedString2 = replaceArbitraryString(string2, replacement);
  10. System.out.println(updatedString1);
  11. System.out.println(updatedString2);
  12. }
  13. public static String replaceArbitraryString(String input, String replacement) {
  14. String pattern = "src=\"[^\"]+\"";
  15. Pattern regexPattern = Pattern.compile(pattern);
  16. Matcher matcher = regexPattern.matcher(input);
  17. if (matcher.find()) {
  18. String found = matcher.group();
  19. return input.replace(found, "src=\"" + replacement + "\"");
  20. }
  21. return input;
  22. }
  23. }

这段代码可以用来替换类似于 "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:

  1. String string1 = "text1 src="arbitrary_string_1" text2"
  2. String string2 = "text3 src="arbitrary_string_2" text4"

to:

  1. string1 = "text1 src="stackoverflow" text2"
  2. string2 = "text3 src="stackoverflow" text4"

I tried several codes using substring() function like:

  1. String string1 = "text1 src=\"arbitrary string\" text2" \\declaration of string1
  2. 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

看一下这个:

  1. public static String replaceAbitrary(String input, String replacement) {
  2. // 从开头到第一个 " 的部分。如果你不想在结果中包含 ",可以移除 +1
  3. String start = input.substring(0, input.indexOf('\"') + 1);
  4. // 从最后一个 " 开始的部分。如果你不想在结果中包含 ",添加一个 +1
  5. String end = input.substring(input.lastIndexOf('\"'));
  6. // 把它们组合起来
  7. return start + replacement + end;
  8. }
  9. public static void main(String[] args) throws IOException {
  10. // 输入部分。注意,这个系统甚至允许任意字符串中包含引号。
  11. String input = "text1 src=\"arbitrary \"string\"\" text2";
  12. // 要替换成的字符串。
  13. String replacement = "stackoverflow";
  14. System.out.println(replaceAbitrary(input, replacement));
  15. }

这会输出 text1 src="stackoverflow" text2。如果你不想包含引号,按照代码内的指示操作。

英文:

Take a look at this:

  1. public static String replaceAbitrary(String input, String replacement) {
  2. // The input up to the first ". If you don't want to include the " in you
  3. // result, remove the +1
  4. String start = input.substring(0, input.indexOf('"') + 1);
  5. // The input starting from the last ". If you don't want to include the ", add a
  6. // +1
  7. String end = input.substring(input.lastIndexOf('"'));
  8. // Put it all together
  9. return start + replacement + end;
  10. }
  11. public static void main(String[] args) throws IOException {
  12. // The input. Note that this system even allows quotation marks inside the
  13. // arbitrary string.
  14. String input = "text1 src=\"arbitrary \"string\"\" text2";
  15. // The string to put instead of the arbitrary one.
  16. String replacement = "stackoverflow";
  17. System.out.println(replaceAbitrary(input, replacement));
  18. }

This outputs text1 src="stackoverflow" text2. If you don't want the quotation marks included, follow the instructions inside the code.

huangapple
  • 本文由 发表于 2020年5月19日 17:56:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/61888192.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定