import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
public static void main(String[] args) {
final String regex = "<\\s*tag\\b[^>]*?(?<=\\s)attribute1\\s*=\\s*\\\"([^\\\"]*)\\\"[^>]*>";
final String string = "<tag attribute1=\"'text123'\" />\n\n"
+ "<TAG attribute2=\"'true'\" attribute1=\"'text1'\"/>\n\n"
+ "<tag attribute2=\"'true'\"/>\n\n"
+ "< tag\n"
+ " type=\"apple\" title=\"Golden apple\"\n"
+ " ATTRIBUTE1=\"value\" />\n\n"
+ "<!-- This one should not match -->\n"
+ "<tagger attribute1=\"something\">\n"
+ " <!-- But this one yes -->\n"
+ " <tag data-id=\"6735\"\n"
+ " attribute1=\"'some more text'\"\n"
+ " required >\n"
+ " Whatever in here\n"
+ " </tag>\n"
+ "</tagger>\n\n"
+ "<!--\n"
+ "Should not match if \"attribute1\" is part of an other attribute name.\n"
+ "As attributes can contain a hyphen, we can't use \\battribute1 because \\b\n"
+ "will not consider the hyphen as part of a word. This is why we have to\n"
+ "use the positive lookbehind (?<=\\s)\n"
+ "-->\n"
+ "<tag matching=\"false\" data-attribute1=\"\" />";
final Pattern pattern = Pattern.compile(regex, Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CHARACTER_CLASS);
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