0

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

5
  • 6
    what did you try so far? what problem did you face? Or you just want us to write a code for you? Commented Apr 15, 2016 at 16:08
  • Is it alright if the trailing ? is still there, i.e. some-image.jpg?" class=" Commented Apr 15, 2016 at 16:11
  • The images are always in jpg format? Commented Apr 15, 2016 at 16:19
  • Since the "?resize="-part stays the same you should have no problem to find it. Commented Apr 15, 2016 at 16:23
  • I think the following answer will be helpful: stackoverflow.com/questions/7018952/java-regex-replace Commented Apr 15, 2016 at 16:27

2 Answers 2

0

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);
Sign up to request clarification or add additional context in comments.

Comments

0

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;" />

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.