正则表达式问题?

aruoyx 2007-07-14 07:46:06
在JAVA 中,想一个编辑框输入内容,如果一遇到输入的字符不属于字符串或数字或'_',则马上提示错误,自动清空刚才输入的错误字符,如何实现?
...全文
286 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
MicroSoftor 2007-07-18
  • 打赏
  • 举报
回复
[^a-zA-Z0-9_]
正则只能匹配,不能帮你删除清空
配合后台正好
myppfly 2007-07-17
  • 打赏
  • 举报
回复
好像应该不是用正则吧
shan1119 2007-07-17
  • 打赏
  • 举报
回复
\\w就可以匹配吧.
believefym 2007-07-17
  • 打赏
  • 举报
回复
你怎么弄的?
aruoyx 2007-07-16
  • 打赏
  • 举报
回复
还是不正确,要求是向编辑框输入内容时,一遇到不是数字或者26个字母的大小写之一或‘_’线就提示错误,阻止继续输入
believefym 2007-07-15
  • 打赏
  • 举报
回复
/**
* this class is used to restrict the input, it makes sure that
* the text of the text field is a digit string.that is, 'a','Z',
* '_','$'and so on are all forbidden.
* @author feng
*
*/
private class OnlyDigit extends PlainDocument{
private JTextField f;

public OnlyDigit(JTextField f){
this.f = f;
}
public void insertString(int offset,
String str,
AttributeSet attSet)
throws BadLocationException{
StringBuffer tmp = new StringBuffer(f.getText());
tmp.insert(offset,str);
Pattern p = Pattern.compile("^-?\\d*(\\.)?\\d*$");
Matcher m = p.matcher(tmp.toString());
if(m.find()){
super.insertString(offset,str,attSet);
}
}
}



----------------

a = new JTextField(5);
//......
a.setDocument(new OnlyDigit(a));
xiaoxiao8372 2007-07-15
  • 打赏
  • 举报
回复
你自己用动脑想想,正则是对字符串的处理,而你所说的是敲一个键判断一下.
aruoyx 2007-07-14
  • 打赏
  • 举报
回复
我想用正则表达式实现,在SWING中设计
xiaoxiao8372 2007-07-14
  • 打赏
  • 举报
回复
主要是给文本框加个键盘监听器
public void keyReleased(KeyEvent e)
{

}
public void keyTyped(KeyEvent e)
{
// text1.setText("123456789");
}
public void keyPressed(KeyEvent e)
{
char k=e.getKeyChar();
if ((k>='A' && k<= 'z') || (k>='0' && k<='9')|| k == '_')
{
//JOptionPane.showMessageDialog(null,"正常");

}
else
text1.setText("");
}
mathfox 2007-07-14
  • 打赏
  • 举报
回复
你说的是用AWT
还是在网页中啊?
要是用的是AWT.SWING的话,

用TextListener 中的void textValueChanged(TextEvent e) 方法。
Pattern p = Pattern("[^w\-]");
应该用这个吧,。初学。
tomuno 2007-07-14
  • 打赏
  • 举报
回复
js 事件处理
lanseliuying 2007-07-14
  • 打赏
  • 举报
回复
数字或'_'可以判断,字符串你意思是什么?所有的输入都可以认为是字符串

62,623

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧