2015年8月24日
17:25
matches:判断是否匹配给出的正则表达式
String rex ="[a-zA-Z0-9]{3,20}@[a-zA-Z0-9]{2,10}[.](com|cn|net)";//正则表达式 格式
importjavax.swing.JOptionPane;
public class Myrex {
/**
* @param args
*/
publicstatic void main(String[] args) {
//TODO Auto-generated method stub
String a =JOptionPane.showInputDialog(null,"请输入电子邮件");
Stringrex = "[a-zA-Z0-9]{3,20}@[a-zA-Z0-9]{2,10}[.](com|cn|net)";
//判断字符串a,是否匹配rex正则表达式描述的格式
if(a.matches(rex)){
JOptionPane.showMessageDialog(null,"合法");
}else{
JOptionPane.showMessageDialog(null,"不合法");
}
}
}
[ ]:[ ]中描述的是任意字符
{ 3,10}:最少出现3次,最多出现10次
{3,} :最少出现3次,没有上限限制
{5} :只能出现5次 ,不能多 ,不能少
|:或者