一个关于java正则表达式的问题,感觉就不起什么作用

「已注销」 2016-05-20 09:22:49
写一个用户注册页面,验证用户输入的用户名,要求只能输入中英文数字字符,其余的字符不能匹配
写了一个函数如下,但是不管输入什么都能通过验证,感觉这个函数的正则表达式就不起作用
代码如下:
  public boolean checkuname(String strname  )      // 通过正则表达式验证输入用户名的正确性
{
Pattern pname=Pattern.compile("[^\u4e00-\u9fa5A-Za-z0-9]"); //^[a-zA-Z0-9\u4E00-\u9FA5]+$ 验证中英文数字字符非要这么写搞不懂
Matcher m=pname.matcher("strname");
boolean regxname=m.matches();

if(strname.length()==0)
{ System.out.println("用户名不能为空!");
return false;
} else if(regxname)
{ System.out.println("用户名输入了非法字符"+strname+regxname);
return false;
}else if((strname.length()<2)||(strname.length()>20))
{ System.out.println("用户名长度必须在2个字符到20个字符之间!");
return false;
}else { System.out.println(regxname+" "+strname );
System.out.println("用户名验证成功!");
return true;
}
}
...全文
303 14 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
anakin_feng 2016-05-20
  • 打赏
  • 举报
回复

Pattern pname = Pattern.compile("^[a-zA-Z0-9]{2,20}");//这样就是2-20个
anakin_feng 2016-05-20
  • 打赏
  • 举报
回复
public static boolean checkuname(String strname)      // 通过正则表达式验证输入用户名的正确性
    {
        Pattern pname = Pattern.compile("[a-zA-Z0-9]+$");   //这里是大小写字母和数字
        Matcher m = pname.matcher(strname);
        boolean regxname = m.matches();
        System.out.print(strname+"    ");
        if (strname.length() == 0) {
            System.out.println("用户名不能为空!");
            return false;
        } else if (!regxname) {//这里取反
            System.out.println("用户名输入了非法字符");
            return false;
        } else if ((strname.length() < 2) || (strname.length() > 20)) {
            System.out.println("用户名长度必须在2个字符到20个字符之间!");
            return false;
        } else {
            System.out.println("用户名验证成功!");
            return true;
        }
    }
anakin_feng 2016-05-20
  • 打赏
  • 举报
回复
public static boolean checkuname(String strname) // 通过正则表达式验证输入用户名的正确性 { Pattern pname = Pattern.compile("[a-zA-Z0-9]+$"); //这里是大小写字母和数字 Matcher m = pname.matcher(strname); boolean regxname = m.matches(); System.out.print(strname+" "); if (strname.length() == 0) { System.out.println("用户名不能为空!"); return false; } else if (!regxname) {//这里取反 System.out.println("用户名输入了非法字符"); return false; } else if ((strname.length() < 2) || (strname.length() > 20)) { System.out.println("用户名长度必须在2个字符到20个字符之间!"); return false; } else { System.out.println("用户名验证成功!"); return true; } }
rickylin86 2016-05-20
  • 打赏
  • 举报
回复

import java.util.regex.Pattern;
import java.util.regex.Matcher;

public class Test{
	public static void main(String[] args){
		String username = "";
		//长度2-20
		//不以数字开头
		//只能包含中文/英文/数字
		String regex = "^(?!\\d)([\u4E00-\u9FA5]|\\w){2,20}$";
		Matcher matcher = Pattern.compile(regex).matcher(username);
		if(!matcher.matches()){
			System.out.printf("username:'%s' is invalid!%n",username);
		}else{
			System.out.printf("username:'%s' is valid!%n",username);
		}
	}
}
RunningCamel 2016-05-20
  • 打赏
  • 举报
回复
1、红色地方验证有问题吧,应该是!regxname
if(strname.length()==0)
      {   System.out.println("用户名不能为空!");
          return false;  
        } else if(regxname)
        {  System.out.println("用户名输入了非法字符"+strname+regxname);
            return false;
        }else if((strname.length()<2)||(strname.length()>20))
        {   System.out.println("用户名长度必须在2个字符到20个字符之间!");
            return false;
        }else {  System.out.println(regxname+"   "+strname );
            System.out.println("用户名验证成功!");
            return true;
            }
    }
2、
("[^\u4e00-\u9fa5A-Za-z0-9]")
 
后面应该有个+号,表示多个数字或者字母。("[^\u4e00-\u9fa5A-Za-z0-9]+") 3、你代码能不能写规范点。。。
anakin_feng 2016-05-20
  • 打赏
  • 举报
回复

  public boolean checkuname(String strname  )      // 通过正则表达式验证输入用户名的正确性
    {    
Pattern pname=Pattern.compile("[^\u4e00-\u9fa5A-Za-z0-9]");   //^[a-zA-Z0-9\u4E00-\u9FA5]+$ 验证中英文数字字符非要这么写搞不懂
Matcher m=pname.matcher("strname");//这个怎么能用引号

  boolean regxname=m.matches();
 
   if(strname.length()==0)
      {   System.out.println("用户名不能为空!");
          return false;  
        } else if(regxname)
        {  System.out.println("用户名输入了非法字符"+strname+regxname);
            return false;
        }else if((strname.length()<2)||(strname.length()>20))
        {   System.out.println("用户名长度必须在2个字符到20个字符之间!");
            return false;
        }else {  System.out.println(regxname+"   "+strname );
            System.out.println("用户名验证成功!");
            return true;
            }
    }
「已注销」 2016-05-20
  • 打赏
  • 举报
回复
搞定 谢谢各位 特别是rickylin86 Molly_1994
rickylin86 2016-05-20
  • 打赏
  • 举报
回复
引用 7 楼 skyrain19782004 的回复:
[quote=引用 3 楼 rickylin86 的回复:]

import java.util.regex.Pattern;
import java.util.regex.Matcher;

public class Test{
	public static void main(String[] args){
		String username = "";
		//长度2-20
		//不以数字开头
		//只能包含中文/英文/数字
		String regex = "^(?!\\d)([\u4E00-\u9FA5]|\\w){2,20}$";
		Matcher matcher = Pattern.compile(regex).matcher(username);
		if(!matcher.matches()){
			System.out.printf("username:'%s' is invalid!%n",username);
		}else{
			System.out.printf("username:'%s' is valid!%n",username);
		}
	}
}
你这个我刚试用过了 还是不行例如我输入sfd&dsh()d 的时候 仍然验证通过,我想输入任何非中英文数字字符时就验证不通过。[/quote] 你试下用"sfd&dsh()d"赋值content试下.没问题的. 应该是你读取数据的代码有问题.
kutuzov2016 2016-05-20
  • 打赏
  • 举报
回复
public boolean checkuname(String name) { String regex ="[\u4e00-\u9fa5A-Za-z0-9]{2,20}"; return name.matches(regex); }
「已注销」 2016-05-20
  • 打赏
  • 举报
回复
我刚查了 matches 方法的api说明 matches public boolean matches() 尝试将整个区域与模式匹配。 如果匹配成功,则可以通过 start、end 和 group 方法获取更多信息。 返回: 当且仅当整个区域序列匹配此匹配器的模式时才返回 true。 这个意思 是不是表示 只有整个字符串匹配正则表达式才返回true啊
「已注销」 2016-05-20
  • 打赏
  • 举报
回复
各位的回复我都看了 也都试过了 ,貌似不行,我的需求是当用户名的字符串 在任何地方输入非中英文数字字符时验证不通过,我这是做的后台验证,我在前端用javascript代码验证完全没有问题,javascript代码如下
var re = /[^\w\u4e00-\u9fa5]/g
		if (re.test(username.value)) {
			pusernamemsg.innerHTML = "含有非法字符!";
			return false;
javascript的正则表达式这么写没有任何问题 ,我知道java和javascipt下正则的语法区别,在java下这么写[^\\w\u4e00-\u9fa5] 我想应该表示 非A-Za-z0-9以及非中文字符 可就是验证不能通过,我输入了以下几组测试 skyrain skyr&^fds **&^^#$($ 竟然都能通过 最奇怪的是全部为**&^^#$($ 这样的字符 竟然也能验证通过。
anakin_feng 2016-05-20
  • 打赏
  • 举报
回复
Pattern pname = Pattern.compile("^[a-zA-Z0-9]+$");
加个^
「已注销」 2016-05-20
  • 打赏
  • 举报
回复
引用 4 楼 Molly_1994 的回复:
public static boolean checkuname(String strname) // 通过正则表达式验证输入用户名的正确性 { Pattern pname = Pattern.compile("[a-zA-Z0-9]+$"); //这里是大小写字母和数字 Matcher m = pname.matcher(strname); boolean regxname = m.matches(); System.out.print(strname+" "); if (strname.length() == 0) { System.out.println("用户名不能为空!"); return false; } else if (!regxname) {//这里取反 System.out.println("用户名输入了非法字符"); return false; } else if ((strname.length() < 2) || (strname.length() > 20)) { System.out.println("用户名长度必须在2个字符到20个字符之间!"); return false; } else { System.out.println("用户名验证成功!"); return true; } }
当我输入 &sky7&fsa 如下用户名时验证仍然通过,可我的需求是用户名字符串的任何地方输入非中英数字字符时验证不通过。
「已注销」 2016-05-20
  • 打赏
  • 举报
回复
引用 3 楼 rickylin86 的回复:

import java.util.regex.Pattern;
import java.util.regex.Matcher;

public class Test{
	public static void main(String[] args){
		String username = "";
		//长度2-20
		//不以数字开头
		//只能包含中文/英文/数字
		String regex = "^(?!\\d)([\u4E00-\u9FA5]|\\w){2,20}$";
		Matcher matcher = Pattern.compile(regex).matcher(username);
		if(!matcher.matches()){
			System.out.printf("username:'%s' is invalid!%n",username);
		}else{
			System.out.printf("username:'%s' is valid!%n",username);
		}
	}
}
你这个我刚试用过了 还是不行例如我输入sfd&dsh()d 的时候 仍然验证通过,我想输入任何非中英文数字字符时就验证不通过。

62,635

社区成员

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

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