使用Regex突出显示JTextPane中的数字

时间:2014-04-13 09:37:20

标签: java regex jtextpane

我试图突出显示在JTextPane中写的数字。

这是我的代码:

// Highlight.java

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;

class Highlight

{

 public static void main(String[] abc)

 {

  JFrame frame = new JFrame("Highlighting");
  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

  JTextPane textPane = new JTextPane();
  textPane.setDocument(new NumberHighlight());

  frame.add(textPane);

  frame.setSize(450,450);
  frame.setLocationRelativeTo(null);
  frame.setVisible(true);

 }

}

// NumberHighlight.java

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.text.*;
import javax.swing.text.MutableAttributeSet.*;

class NumberHighlight extends DefaultStyledDocument

{
private static final  MutableAttributeSet BOLD = new SimpleAttributeSet();

 private static int findLastNonWordChar (String text, int index)
 {
  while (--index >= 0)
  {
   if (String.valueOf(text.charAt(index)).matches("\\W"))
   {
    break;
   }
  }
  return index;
 }

 private static int findFirstNonWordChar (String text, int index)
 {
  while (index < text.length())
  {
   if (String.valueOf(text.charAt(index)).matches("\\W"))
   {
    break;
   }
   index++;
  }
  return index; 
 }
  final StyleContext cont = StyleContext.getDefaultStyleContext();

  final AttributeSet attp = cont.addAttribute(cont.getEmptySet(), StyleConstants.Foreground, new Color(255,0,255));
  final AttributeSet attrBlack = cont.addAttribute(cont.getEmptySet(), StyleConstants.Foreground, Color.BLACK);

  public void insertString (int offset, String str, AttributeSet a) throws BadLocationException
  {
   super.insertString(offset, str, a);
   String text = getText(0, getLength());
   int before = findLastNonWordChar(text, offset);
   if (before < 0) before = 0;
   int after = findFirstNonWordChar(text, offset + str.length());
   int wordL = before;
   int wordR = before;
   while (wordR <= after)
   {
    if (wordR == after || String.valueOf(text.charAt(wordR)).matches("\\W"))
    {
     if (text.substring(wordL, wordR).matches("(\\W)*(\\d+$)"))
     {
       setCharacterAttributes(wordL, wordR - wordL, BOLD, false);
       setCharacterAttributes(wordL, wordR - wordL, attp, false);
     }      
     else
     {
      StyleConstants.setBold(BOLD, false);
      setCharacterAttributes(wordL, wordR - wordL, BOLD, true);
      setCharacterAttributes(wordL, wordR - wordL, attrBlack, false);
     }  
     wordL = wordR;
    }
    wordR++;
   }
  }
  public void remove (int offs, int len) throws BadLocationException
  {
   super.remove(offs, len);
   String text = getText(0, getLength());
   int before = findLastNonWordChar(text, offs);
   if (before < 0) before = 0;
   int after = findFirstNonWordChar(text, offs);
   if (text.substring(before, after).matches("(\\W)*(\\d+$)"))
   {
    setCharacterAttributes(before, after - before, BOLD, false);
    setCharacterAttributes(before, after - before, attp, false);
   } 
   else
   {
    StyleConstants.setBold(BOLD, false);
    setCharacterAttributes(before, after - before, BOLD, true);
    setCharacterAttributes(before, after - before, attrBlack, false);
   }
  }

 }

我正面临着我的正则表达式的问题。一切正常但如果我在数字前写一个字母数字字符,那么字母数字字符的属性也会改变。

如果在数字后插入字母数字字符,我就不会遇到此问题。

示例:

JTextPaneregex

我的正则表达式出了什么问题?

谢谢!

1 个答案:

答案 0 :(得分:1)

我不太明白你的目标是什么,但我可以告诉你正则表达式在做什么,希望这对你有帮助。

String.valueOf(text.charAt(index)).matches("\\W")

这需要一个字符,如果是not a word character(单词字符是字母,数字或下划线),则返回true。

text.substring(wordL, wordR).matches("(\\W)*(\\d+$)"))

如果子串

,则返回true
  1. 以零个或多个非单词字符开头:(\\W)*
  2. 后面跟着一个 - 或者更多digits(\\d+$)
  3. 此匹配必须锚定到string start and end。你的正则表达式中的美元符号与matches函数是多余的,尽管它没有任何害处。
  4. 因为它已锚定,所以字符串@*#&$%^@( ||3@3匹配。

    这些字符串不匹配:3@(因为它以非数字结尾),3@3(因为它以单词字符开头 - 数字)。

    capture groups也没有用,但同样,它们没有任何伤害。换句话说,您也可以使用\\W*\\d+

    ^$位于以下正则表达式中,以模拟matches所需的锚定。)

    ^(\W)*(\d+$)
    

    Regular expression visualization

    Debuggex Demo

    ^\W*\d+$
    

    Regular expression visualization

    Debuggex Demo


    请考虑将Stack Overflow Regular Expressions FAQ加入书签以供将来参考。这个答案中的所有链接都来自它。