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
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);
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;" />
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.