0
votes

I'm getting the RSS feeds from a wordpress blog where I get the thumbnail image in the string. Below is the sample feed i get

<img src="http://www.example.com/some-image.jpg?resize=50%2C50" class="attachment-thumbnail wp-post-image" alt="SomeImage" style="margin:0px;" />

I need to remove "?resize=50%2C50" from the image source. But the problem is I can't hardcode this in my code as the size may not remain the same. Also the order in which the attributes are placed may change

How can I simply remove anything that matches this pattern so that I can always get the output as

<img src="http://www.example.com/some-image.jpg" class="attachment-thumbnail wp-post-image" alt="Some Image" style="margin:0px;" />

Thanks in advance

2
what did you try so far? what problem did you face? Or you just want us to write a code for you? - Alex Salauyou
Is it alright if the trailing ? is still there, i.e. some-image.jpg?" class=" - Darth Android
The images are always in jpg format? - Ricardo
Since the "?resize="-part stays the same you should have no problem to find it. - Martin
I think the following answer will be helpful: stackoverflow.com/questions/7018952/java-regex-replace - yk11

2 Answers

0
votes

RegEx to capture your image: src=(".+\.jpg)(\?resize\S+") Can then replace with src=\$1"

    String url="<img src=\"http://www.example.com/some-image.jpg?resize=50%2C50\" class=\"attachment-thumbnail wp-post-image\" alt=\"SomeImage\" style=\"margin:0px;\" />";
    final String regex="src=(\".+\\.jpg)(\\?resize\\S+\")";
    url = url.replaceFirst(regex, "src=$1\"");

    System.out.println(url);
0
votes

If I understood correctly, you want only the path up to the params, so:

String str = "<img src=\"http://www.example.com/some-image.jpg?resize=50%2C50\" class=\"attachment-thumbnail wp-post-image\" alt=\"SomeImage\" style=\"margin:0px;\" />";
System.out.println(str.replaceFirst("(\\?\\S[^\"]+)", ""));

This will output:

<img src="http://www.example.com/some-image.jpg" class="attachment-thumbnail wp-post-image" alt="SomeImage" style="margin:0px;" />