跪求一个正则表达式 对正则表达式不太熟悉,现在有一个这样的需求,有一个输入框,可以接受一组或者多组3~21位之间的数字,如果多与一组之间用逗号隔开举例: 1:345464564 2:46456456,46457657567,23423432大概就是这个意思,多谢各位了 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 public class TestRegex{ /** * @param args */ public static void main(String[] args) { System.out.println(Regex()); } private static boolean Regex() { String str = "6666664a,2224"; String[] strSplit = str.split(","); Matcher m = null; int length = strSplit.length; if (length > 1) { for (int i = 0; i < length; i++) { m = Pattern.compile("\\d{3,21}").matcher(strSplit[i]); if (!m.matches()) { return false; } } } else { m = Pattern.compile("\\d{3,21}").matcher(str); } return m.matches(); }}可能不是最好的,但是能满足你的需求, public class TestRegex{ /** * @param args */ public static void main(String[] args) { System.out.println(Regex()); } private static boolean Regex() { String str = "6666664,444,333"; Matcher m = Pattern.compile("(\\d{3,21})(,\\d{3,21})*").matcher(str); return m.matches(); }}这个效率高些, 呵呵 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="zh-CN" dir="ltr"> <head> <meta http-equiv="Content-Type" content="text/html; charset=gbk" /> </head><style type="text/css">*{font-size:12px;font-family:"courier new";}input.text{width:200px;height:20px;border:1px solid #c0c0c0;padding:1px 3px;}span.msg{color:red;padding:0 10px;}</style><script type="text/javascript">window.$ = function(id) { return (typeof id == 'string') ? document.getElementById(id) : id;}String.prototype.trim = function() { return this.replace(/^\s+/, '').replace(/\s+$/, '');}window.onload = function() { hover();}function hover() { var inputs = document.getElementsByTagName('input'); if(!inputs) { return; } var color = '#ffee99'; for(var i = 0; i < inputs.length; i++) { if(inputs[i].type != 'text') { continue; } inputs[i].onmouseover = inputs[i].onfocus = function() { this.style.backgroundColor = color; } inputs[i].onmouseout = function() { this.style.backgroundColor = 'transparent'; } }}function check(frm) { var inp = frm.input; var v = inp.value.trim(); inp.value = v; if(!/^[0-9]{3,21}(?:,[0-9]{3,21})*$/.test(v)) { $('inputMsg').innerHTML = '格式错误,请重新输入!一组只允许输入 3~21 个数字,多组使用 , 分隔'; inp.focus(); inp.select(); return false; } $('inputMsg').innerHTML = ''; return true;}</script><body> <form name="frm" action="#" method="post" onsubmit="return check(this);"> 输入框: <input type="text" name="input" class="text" /><span id="inputMsg" class="msg"></span><br /> <input type="submit" value="提交" /> </form></body></html> "[a-zA-Z]+[0-9]+"例如password.matches("[a-zA-Z]+[0-9]+"); 字符串password 是否属于a-z A-Z 0-9 (\d{3,21})(,\d{3,21})*(\d{3,21},)*(\d{3,21})都对· public static final String REGEX = "(?:\\d{3,12},*)+"; public static void main(String[] args) { String s = "123464,4232,34234235,453453,3"; Matcher m = Pattern.compile(strRegex).matcher(s); System.out.println(m.matches()); } java中编写一个判断一个字符数组中是否存在某个字符 Java的class能实现运算符override吗? 可以实现A+B的运算。 请问如何在amd64位机子上安装jdk1.5 用java怎么编写并打印下例数字,谢谢? 初学者对访问属性的问题 难倒很多人的一道题 令人厌烦的问题 解析xml文件改为解析xml字符串问题? 这个程序如何修改?请高手帮忙 怎样打破APPLET的安全性,使其本地化(如果安全方面的高手请留OICQ,共同切磋!) 在学习《Java学习笔记(JDK6)》这本书中有几个不明白的问题想请教各位! 给解释一下这个正则表达式。
public class TestRegex
{ /**
* @param args
*/
public static void main(String[] args)
{ System.out.println(Regex()); } private static boolean Regex()
{
String str = "6666664a,2224";
String[] strSplit = str.split(","); Matcher m = null; int length = strSplit.length;
if (length > 1)
{
for (int i = 0; i < length; i++)
{
m = Pattern.compile("\\d{3,21}").matcher(strSplit[i]);
if (!m.matches())
{
return false;
}
}
}
else
{
m = Pattern.compile("\\d{3,21}").matcher(str);
} return m.matches();
}}可能不是最好的,但是能满足你的需求,
public class TestRegex
{ /**
* @param args
*/
public static void main(String[] args)
{ System.out.println(Regex()); } private static boolean Regex()
{
String str = "6666664,444,333"; Matcher m = Pattern.compile("(\\d{3,21})(,\\d{3,21})*").matcher(str);
return m.matches();
}}这个效率高些, 呵呵
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gbk" />
</head><style type="text/css">
*{font-size:12px;font-family:"courier new";}
input.text{width:200px;height:20px;border:1px solid #c0c0c0;padding:1px 3px;}
span.msg{color:red;padding:0 10px;}
</style><script type="text/javascript">
window.$ = function(id) {
return (typeof id == 'string') ? document.getElementById(id) : id;
}String.prototype.trim = function() {
return this.replace(/^\s+/, '').replace(/\s+$/, '');
}window.onload = function() {
hover();
}function hover() {
var inputs = document.getElementsByTagName('input');
if(!inputs) {
return;
}
var color = '#ffee99';
for(var i = 0; i < inputs.length; i++) {
if(inputs[i].type != 'text') {
continue;
}
inputs[i].onmouseover = inputs[i].onfocus = function() {
this.style.backgroundColor = color;
}
inputs[i].onmouseout = function() {
this.style.backgroundColor = 'transparent';
}
}
}function check(frm) {
var inp = frm.input;
var v = inp.value.trim();
inp.value = v;
if(!/^[0-9]{3,21}(?:,[0-9]{3,21})*$/.test(v)) {
$('inputMsg').innerHTML = '格式错误,请重新输入!一组只允许输入 3~21 个数字,多组使用 , 分隔';
inp.focus();
inp.select();
return false;
}
$('inputMsg').innerHTML = '';
return true;
}</script><body>
<form name="frm" action="#" method="post" onsubmit="return check(this);">
输入框: <input type="text" name="input" class="text" /><span id="inputMsg" class="msg"></span><br />
<input type="submit" value="提交" />
</form>
</body>
</html>
例如password.matches("[a-zA-Z]+[0-9]+"); 字符串password 是否属于a-z A-Z 0-9
(\d{3,21},)*(\d{3,21})
都对·
public static void main(String[] args) {
String s = "123464,4232,34234235,453453,3";
Matcher m = Pattern.compile(strRegex).matcher(s);
System.out.println(m.matches());
}