Here you can find the source of applyRegex(String regex, JTextPane pane, Color highlightColor)
public static void applyRegex(String regex, JTextPane pane, Color highlightColor)
//package com.java2s; //License from project: Open Source License import javax.swing.*; import javax.swing.text.AttributeSet; import javax.swing.text.SimpleAttributeSet; import javax.swing.text.StyleConstants; import javax.swing.text.StyleContext; import java.awt.*; import java.util.regex.Matcher; import java.util.regex.Pattern; public class Main { public static void applyRegex(String regex, JTextPane pane, Color highlightColor) { Pattern pattern = Pattern.compile(regex); Matcher matcher = pattern.matcher(pane.getText()); while (matcher.find()) { StyleContext sc = StyleContext.getDefaultStyleContext(); AttributeSet aset = sc.addAttribute(SimpleAttributeSet.EMPTY, StyleConstants.Foreground, highlightColor);// w ww . j a v a 2 s. c o m pane.getStyledDocument().setCharacterAttributes(matcher.start(), matcher.end() - matcher.start(), aset, true); } } }