in src/main/groovy/swing/RegexCoach.groovy [89:125]
private doHighlights() {
try {
resetView()
// note: get the text from the underlying document,
// otherwise carriage return/line feeds different when using the JTextPane text
def regex = swing.regexPane.document.getText(0, swing.regexPane.document.length)
def target = swing.targetPane.document.getText(0, swing.targetPane.document.length)
def matcher = (target =~ regex)
// scan past the matches before the match we want
int scan = 0
while (scan < scanIndex) {
matcher.find()
scan++
}
if (matcher.find()) {
// highlight any captured groups
int i = 0
while (i++ < matcher.groupCount()) {
swing.targetPane.highlighter.addHighlight(matcher.start(i), matcher.end(i), orange)
}
// highlight whole match
swing.targetPane.highlighter.addHighlight(matcher.start(), matcher.end(), yellow)
if (regex.length() != 0) {
swing.targetStatus.text = "Match #${scanIndex + 1} from ${matcher.start()} to ${matcher.end()}."
}
} else { // not found
scanIndex = Math.max(scan - 1, 0)
if (scanIndex > 0) {doHighlights()}
swing.targetStatus.text = "No match."
}
} catch (PatternSyntaxException e) {
swing.regexPane.highlighter.addHighlight(e.index, e.index + 2, red)
swing.regexStatus.text = e.description
}
}