没有见过match(),是不是matcher()?public Pattern.matcher(CharSequence input) 得到一个要比较字符序列是input的比较器(matcher)
public static boolean Pattern.matches(String regex,CharSequence input)
在字符序列input查找正则表达式regex对应的pattern,
等同与Pattern.compile(regex).matcher(input).matches();
如果regex要使用多次,请使用下面的形式,可以省去编译regex的时间
Pattern partn = Pattern.compile(regex);
partn.matcher(input).matches();
public static boolean Pattern.matches(String regex,CharSequence input)
在字符序列input查找正则表达式regex对应的pattern,
等同与Pattern.compile(regex).matcher(input).matches();
如果regex要使用多次,请使用下面的形式,可以省去编译regex的时间
Pattern partn = Pattern.compile(regex);
partn.matcher(input).matches();
如果输入的不是全数字,那么在后台打印"输入的号码位数不正确!"
我不清楚这个matches(String regex)该怎么正确使用.
现在的情况是输入超过8位数就会执行ELSE部份的语句.TextField tf = new TextField();
//一个按纽临听器.
jb1.addMouseListener(new MouseAdapter(){
public void mousePressed(MouseEvent e){
a=tf.getText(); //得到文本输入框的字符串(15位的字符串).
if (a.matches("[0-9]{1,8}"))
{
System.out.print(a);
}
else
{ ystem.out.print("输入的号码位数不正确!");
}
}
});
而不是把一个字符串从要插入的位置做为分界线,用比如substring反回两个字符串,然后在再把它们接起来.谢谢.
out = input.substring(0,i).concat(insert).cancat(input.substring(i));