【问题标题】:Highlighting numbers in JTextPane using Regex使用正则表达式突出显示 JTextPane 中的数字
【发布时间】:2014-04-13 09:37:19
【问题描述】:

我正在尝试突出显示 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);
   }
  }

 }

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

如果在数字后面插入字母数字字符,我不会遇到这个问题。

例子:

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

谢谢!

【问题讨论】:

    标签: java regex jtextpane


    【解决方案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 函数是多余的,尽管它没有害处。

    因为它是锚定的,所以字符串 @*#&amp;$%^@( ||3 匹配,@3 也是如此。

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

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

    ^$ 在下面的正则表达式中,用于模拟matches 要求的锚定。)

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

    Debuggex Demo

    ^\W*\d+$
    

    Debuggex Demo


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

    【讨论】:

    • 虽然它没有准确回答我的问题,但您的回答为我提供了修复正则表达式的重要指示。谢谢您的帮助! SO FAQ 非常宝贵。直到你指点我才知道。
    • 嗯,我很高兴它在某种程度上有所帮助。如果您觉得大方,请考虑给我一个大的绿色复选标记;)
    • 你明白了。你确实帮助了我,看到没有其他答案可用,我会接受你的答案。
    猜你喜欢
    • 1970-01-01
    • 2017-05-11
    • 1970-01-01
    • 2021-01-24
    • 1970-01-01
    • 2017-01-19
    • 2012-01-13
    • 2014-11-01
    • 1970-01-01
    相关资源
    最近更新 更多