// Matcher to find an image tag. The 'html' variable contains the HTML in question Matcher mImg = Pattern.compile("(?id)").matcher(html); // Matcher to find an ALT attribute (to be applied to an IMG tag's body within the same 'html' variable) Matcher mAlt = Pattern.compile("(?ix)\\b ALT \\s* =").matcher(html); // Matcher to find a newline Matcher mLine = Pattern.compile("\\n").matcher(html); // For each image tag within the html . . . while (mImg.find()) { // Restrict the next ALT search to the body of the just-found image tag mAlt.region( mImg.start(1), mImg.end(1) ); // Report an error if no ALT found, showing the whole image tag found above if (! mAlt.find()) { // Restrict counting of newlines to the text before the start of the image tag mLine.region(0, mImg.start()); int lineNum = 1; // The first line is numbered 1 while (mLine.find()) lineNum++; // Each newline bumps up the line number System.out.println("Missing ALT attribute on line " + lineNum); } } ----------------------------------------------------------------------------- Copyright 1997-2024 Jeffrey Friedl