import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
public static void main(String[] args) {
final String regex = "(?:<img|(?<!^)\\G)\\h*([-\\w]+)=\"([^\"]+)\"(?=.*?\\/>)";
final String string = "<p>List of sample images.</p>\n"
+ "<img src=\"https://placehold.it/250x100.jpg\" width=\"250\" height=\"100\" alt=\"Dimensions Set\" />\n"
+ "<img src=\"https://placehold.it/250x100/99cc00/000.jpg?text=JPG\" alt=\"JPG\" /><br>\n"
+ "<img src=\"https://placehold.it/250x100.gif?text=GIF\" alt=\"GIF\" /><br>\n"
+ "<img src=\"https://placehold.it/250x100/ff6600/000.png?text=PNG\" alt=\"PNG\" /><br>\n"
+ "<img class=\"no-ext\" src=\"https://placehold.it/350x150?text=No Extension\" alt=\"No Ext\" /><br>\n"
+ "<img src=\"https://placehold.it/250x100.png\" custom-attr=\"custom1\" another-attr=\"custom2\" /><br>\n"
+ "<img class=\"svg\" src=\"https://upload.wikimedia.org/wikipedia/commons/0/02/SVG_logo.svg\" alt=\"SVG\" /><br>\n"
+ "<img class=\"webp\" src=\"https://gstatic.com/webp/gallery/1.webp\" width=\"100\" alt=\"webP\" /><br>";
final Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE);
final Matcher matcher = pattern.matcher(string);
while (matcher.find()) {
System.out.println("Full match: " + matcher.group(0));
for (int i = 1; i <= matcher.groupCount(); i++) {
System.out.println("Group " + i + ": " + matcher.group(i));
}
}
}
}
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for Java, please visit: https://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html