我正在尝试突出显示 JTextPane 内写入的数字。
这是我的代码:
//高亮.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);
}
}
}
我的正则表达式遇到问题。一切正常,但如果我在数字之前写一个字母数字字符,那么该字母数字字符的属性也会改变。
如果在数字后面插入字母数字字符,我不会遇到此问题。
示例:
我的正则表达式做错了什么?
谢谢!
最佳答案
我不太明白你的目标是什么,但我可以告诉你你的正则表达式在做什么,希望这会对你有所帮助。
String.valueOf(text.charAt(index)).matches("\\W")
这需要一个字符,如果是not a word character则返回true (单词字符是字母、数字或下划线)。
text.substring(wordL, wordR).matches("(\\W)*(\\d+$)"))
如果子字符串返回 true
- 以零个或多个非单词字符开头:
(\\W)*
- 后跟一个或多个 digits 。
(\\d+$)
- 此匹配必须同时锚定到 string start and end 。正则表达式中的美元符号与
matches
是多余的功能,尽管它没有害处。
由于它是锚定的,因此字符串 @*#&$%^@( ||3
会匹配,@3
也会匹配。
这些字符串不匹配:3@
(因为它以非数字结尾)、3@3
(因为它以单词字符 - 数字开头)。
capture groups 也没有用。 ,但同样,它们没有害处。换句话说,您也可以使用 \\W*\\d+
。
(^
和 $
位于下面的正则表达式中,用于模拟 匹配
所需的锚定。)
^(\W)*(\d+$)
^\W*\d+$
<小时/>
请考虑为 Stack Overflow Regular Expressions FAQ 添加书签备查。这个答案中的所有链接都来自它。
关于java - 使用 Regex 在 JTextPane 中突出显示数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23041132/