import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
public static void main(String[] args) {
final String regex = "(?<AttributeName>(?:src|background))\\s*=\\s*[\"'](?<AttributeValue>(?:[^\"'])*)";
final String string = "<html>\n"
+ "<table id='tblTest' border=\"1\">\n"
+ "<tr>\n"
+ "<td colspan=\"1\" >CellA</td>\n"
+ "</tr>\n"
+ "<tr>\n"
+ "<td>Click<a href=\"http://mail.google.com\" >here</a></td>\n"
+ "</tr>\n"
+ "<tr>\n"
+ "<td>\n"
+ "<img src=\"http://wcrustine.org/Portals/0/click_here_button.jpg\" style=\"width:100px;height:100px;\"></img></td>\n"
+ "</tr>\n"
+ "<tr>\n"
+ "<td background=\"https://ci6.googleuser.com/proxy/d-NpaBfbly5hKSic8pomfvI4scwJqkF2wwmRqEB4stR8jfO7jo-Sx-JcVLfIXqw5gJnd05Rl0aY3evT2DhmS6he85ifLGQA=s0-d-e1-ft#http://newsletter.mydomain.com/template/header_bg.jpg\" bgcolor=\"#336699\" height=\"100\" style=\"background:url(https://ci5.googleuser.com/proxy/d-NpaBfbly5hKSic8pomfvI4scwJqkF2wwmRqEB4stR8jJrvfO7jo-Sx-JcVLfIgJnd05Rl0aY3evT2DhmS6LU85ifLGQA=s0-d-e1-ft#http://newsletter.mydomain.com/template/header_bg.jpg) top left repeat #336699\" width=\"\">\n"
+ " \n"
+ "</td>\n"
+ "</tr>\n"
+ "</table>\n"
+ "</html>";
final Pattern pattern = Pattern.compile(regex);
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