これは、JTextFieldがx文字より長くなく、文字またはスペース以外のものを含まないことに対する私の正規表現です。 何らかの理由で、[]文字と\文字を許可しています。 これは私を夢中にさせています。私の正規表現は間違っていますか?
package com.jayavon.game.helper;
import java.awt.Toolkit;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.PlainDocument;
public class CharacterNameCreationDocument extends PlainDocument {
private static final long serialVersionUID = 1L;
private int limit;
public CharacterNameCreationDocument(int limit) {
super();
this.limit = limit;
}
public void insertString(int offset, String str, AttributeSet attr) throws BadLocationException {
if (str == null || (getLength() + str.length()) > limit || !str.matches("[a-zA-z\\s]*")){
Toolkit.getDefaultToolkit().beep();
return;
} else {
super.insertString(offset, str, attr);
}
}
}