在字符之间剪切字符串并且不删除这些字符

时间:2015-01-29 11:16:19

标签: java android regex

问题很简单。我有以下字符串

String str1 = "<img alt=\"Рейтинг \" src=\"https://itvdn.blob.core.windows.net/images/images-bfa852f3-c227-46dd-86d7-2628f7338b0b.png\">";
String str2 = "<img class=\"w640\" id=\"customHeaderImage\" src=\"http://i067.radikal.ru/1412/23/c71d6e8e7d47.jpg\">";

我需要删除imgsrc之间的字符串并保留它们。所以结果应该是这样的:

String str1 = "<img src=\"https://itvdn.blob.core.windows.net/images/images-bfa852f3-c227-46dd-86d7-2628f7338b0b.png\">";
String str2 = "<img src=\"http://i067.radikal.ru/1412/23/c71d6e8e7d47.jpg\">";

我已经尝试过了:

str1 = str1.replaceAll("(img)[^&]*(src)", "");
str2 = str2.replaceAll("img.*?src", "");

但它也删除了imgsrc

目前我正在使用以下解决方案

String firstDelim = "img";
String secondDelim = "src";
int delim1 = str1.indexOf(firstDelim);
int delim2 = str1.indexOf(secondDelim);
if (delim1 >= 0 && delim2 > delim1) {
    str1= str1.substring(0, delim1 + firstDelim.length()) + " " + body.substring(delim2);
}

但我认为还有另一种方式。你可以帮帮我吗?

4 个答案:

答案 0 :(得分:4)

你可以使用&#34; lookarounds&#34;为此:

String str1 = "<img alt=\"Рейтинг \" src=\"https://itvdn.blob.core.windows.net/images/images-bfa852f3-c227-46dd-86d7-2628f7338b0b.png\">";
String str2 = "<img class=\"w640\" id=\"customHeaderImage\" src=\"http://i067.radikal.ru/1412/23/c71d6e8e7d47.jpg\">";
//                                  | preceded by "img"
//                                  |       | any character, reluctantly quantified
//                                  |       |   | followed by "src"
//                                  |       |   |        | replace with 1 space
System.out.println(str1.replaceAll("(?<=img).+?(?=src)", " "));
System.out.println(str2.replaceAll("(?<=img).+?(?=src)", " "));

<强>输出

<img src="https://itvdn.blob.core.windows.net/images/images-bfa852f3-c227-46dd-86d7-2628f7338b0b.png">
<img src="http://i067.radikal.ru/1412/23/c71d6e8e7d47.jpg">

注意

使用正则表达式进行标记解析绝不是一个好主意。但在这种情况下,更换很简单。但要注意边缘情况!

答案 1 :(得分:2)

确实非常简单。你快到了:

str1 = str1.replaceAll("(img)[^&]*(src)", "img src");

答案 2 :(得分:1)

(?<=\bimg\b).*?(?=\bsrc\b)

试试这个。space。见。演示。

https://regex101.com/r/pM9yO9/23

对于java,它将是

 (?<=\\bimg\\b).*?(?=\\bsrc\\b)

这里我们使用0 width assertions而不是消耗字符串本身。

(?&lt; = \ bimg \ b)lookbehind to check string to be captured has img before it.

(?= \ bsrc \ b)lookahead to see if string being captured has src ahead of it.

答案 3 :(得分:0)

谢谢大家,所有答案都有效。不幸的是,我无法将所有答案标记为正确。我使用System.out.println(str1.replaceAll("(?<=img).+?(?=src)", " "));,因此将其标记为正确。 但我感谢大家的所有答案,他们真的很好!