Java 为JTextPane中的正则表达式单词模式着色无法为尾部分隔符着色

Java 为JTextPane中的正则表达式单词模式着色无法为尾部分隔符着色,java,regex,jtextpane,Java,Regex,Jtextpane,多亏了名单上的好人,我几乎做到了。我只需要最后一点的帮助 我有一个JTextPane,我在其中匹配某些单词模式,并将它们的颜色更改为红色。任何以pipe/open square brace开头,以close square brace/pipe结尾的单词或短语,如|[this]|,都与以下正则表达式匹配:“(\\\\[^\\]*\])” 这可以工作99%,但它不会将尾部染成红色。如果我试图将其包含在正则表达式中,在末尾添加一个\\\\\\\\\,它将无法匹配任何内容 这是我的密码 在调用该类的按钮

多亏了名单上的好人,我几乎做到了。我只需要最后一点的帮助

我有一个JTextPane,我在其中匹配某些单词模式,并将它们的颜色更改为红色。任何以pipe/open square brace开头,以close square brace/pipe结尾的单词或短语,如|[this]|,都与以下正则表达式匹配:“(\\\\[^\\]*\])”

这可以工作99%,但它不会将尾部染成红色。如果我试图将其包含在正则表达式中,在末尾添加一个
\\\\\\\\\
,它将无法匹配任何内容

这是我的密码

在调用该类的按钮中,我暂时发送了一个带有分隔符的愚蠢字符串:

private void jButton13ActionPerformed(java.awt.event.ActionEvent evt) {                                          

    JFrame Escapedframe = new JFrame("Skipppy Cars word changer");
    Escapedframe.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

    JTextPane SkippytextPane = new JTextPane();
    SkippytextPane.setDocument(new ElementHighlight());
    SkippytextPane.setText("You should try the |[new model]| car, the |[Skippy 2000]|. The Skippy 2000 comes standard with all the |[features]| of the Bippy 3000, at half the price. You can buy one today at your |[athorized Skippy/Bippy]| dealer.");
    Escapedframe.add(SkippytextPane);

    Escapedframe.setSize(500,400);
    Escapedframe.setLocationRelativeTo(null);
    Escapedframe.setVisible(true);

}  
然后设置:

private int pipeCharEnd (String Xtext, int index) {
    while (--index >= 0) {
        if (String.valueOf(Xtext.charAt(index)).matches("\\|")) {
            break;
        }
    }
    return index;
}

private int pipeCharStart (String Xtext, int index) {
    while (index < Xtext.length()) {
        if (String.valueOf(Xtext.charAt(index)).matches("\\|")) {
            break;
        }
        index++;
    }
    return index;
}
private int-pipeCharEnd(字符串Xtext,int-index){
而(--index>=0){
if(String.valueOf(Xtext.charAt(index)).matches(“\\\\”){
打破
}
}
收益指数;
}
私有int-pipeCharStart(字符串Xtext,int-index){
while(索引
最后是课堂:

class ElementHighlight extends DefaultStyledDocument {
  private final  MutableAttributeSet XRED = new SimpleAttributeSet();
  final StyleContext myProtected = StyleContext.getDefaultStyleContext();
  final AttributeSet attR = myProtected.addAttribute(myProtected.getEmptySet(), StyleConstants.Foreground, Color.RED);
  final AttributeSet attB = myProtected.addAttribute(myProtected.getEmptySet(), StyleConstants.Foreground, Color.BLUE);

@Override
  public void insertString (int offset, String Pstr, AttributeSet RedBlue) throws BadLocationException
  {
   super.insertString(offset, Pstr, RedBlue);
   String text = getText(0, getLength());
   int pre = pipeCharEnd(text, offset);
   if (pre < 0) pre = 0;
   int post = pipeCharStart(text, offset + Pstr.length());
   int prewords = pre;
   int postwords = pre;

   while (postwords <= post) {
                if (postwords == post || String.valueOf(text.charAt(postwords)).matches("\\|")) {
                    if (text.substring(prewords, postwords).matches("(\\|\\[[^\\]]*\\])"))
                        setCharacterAttributes(prewords, postwords - prewords, attR, false);
                    else
                        setCharacterAttributes(prewords, postwords - prewords, attB, false);
                    prewords = postwords;
                }
                postwords++;
            }


  }


 }
class ElementHighlight扩展了DefaultStyledDocument{
private final MutableAttributeSet XRED=新的SimpleAttributeSet();
final StyleContext myProtected=StyleContext.getDefaultStyleContext();
最终属性set attR=myProtected.addAttribute(myProtected.getEmptySet(),StyleConstants.Foreground,Color.RED);
最终属性集attB=myProtected.addAttribute(myProtected.getEmptySet(),StyleConstants.Foreground,Color.BLUE);
@凌驾
public void insertString(int offset、String Pstr、AttributeSet RedBlue)引发BadLocationException
{
super.insertString(偏移量、Pstr、红蓝);
String text=getText(0,getLength());
int pre=pipeCharEnd(文本,偏移量);
如果(pre<0)pre=0;
int post=pipeCharStart(text,offset+Pstr.length());
int-prewords=pre;
int postwords=pre;

while(postwords,因为您输入了以下
if
条件:

text.substring(prewords, postwords).matches("(\\|\\[[^\\]]*\\])")
…这意味着索引
prewords
postword-1
(包括)之间的子
字符串
匹配
(\\\\[^\\]*\])
。此正则表达式查找:

  • a
    |
    后跟
  • a
    [
    后跟
  • ]
    之外的任何字符的序列,后跟
  • a
    ]
因此,考虑到这个示例
字符串

0123|[6789012345678]|12345
正则表达式将匹配
|[6789012345678]
(而不是尾随的
|
)。因此,我们将进入
if
条件,其中
前置词
值为
4
,而
后置词
设置为
20

prewords = 4, postwords = 20
    v               v
0123|[6789012345678]|12345
如果您这样做:

setCharacterAttributes(prewords, postwords - prewords, attR, false);
…然后你宣布:

  • postwords-prewords
    字符
  • 从索引
    prewords开始
  • 将为红色
…这是索引
4
中的
20-4
(16)个字符,在我们的示例
String
中,它表示以下子
String
|[6789012345678]
(仅16个字符)

如果还想设置一个或多个字符的颜色…只需将
+1
添加到方法调用中即可

i、 e:


我必须留下来,我必须读两遍。这并不难理解。我必须读两遍,因为我惊讶于答案是多么丰富、详细、准确和完美。你提供了所有的细节。你的答案完美地解决了我的问题,但更重要的是,它教会了我。真诚的感谢祝你@ccjmne!
//                                                   here
//                                                     v
setCharacterAttributes(prewords, postwords - prewords +1, attR, false);