求一个正则表达式匹配112233

神奇的小喻_Orz 2013-06-23 02:40:15
公司目前要生成一批卡号,希望可以匹配AABB这种格式。
我写了一个正则:(?:[0-9]+)*([\\d])\\1{1,}([\\d])\\2{1,}(?:[0-9]+)*
此正则表达式的意思是:只要卡号里面含带AABB的就可以通过。
经过测试:5201122能过;5201133也能过。

我想要的效果是:5201122能过;5201133不能过。 因为1和2是连号,1和3不是连号。

求大神帮助。
...全文
981 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
小猴168 2013-06-24
  • 打赏
  • 举报
回复
试试这个:
public static void main(String[] args) {
		
		String str = "5201122";
		Pattern p = Pattern.compile("\\d*(\\d)\\1(\\d)\\2\\d*");
		Matcher m = p.matcher(str);
		if(m.find()){
			String s1 = m.group(1);
			String s2 = m.group(2);
			if (Integer.parseInt(s1)+1==Integer.parseInt(s2)){
				System.out.println(m.group());
			}
		}
	}
xuzuning 2013-06-24
  • 打赏
  • 举报
回复
那你就应该在 JAVA 版面求解 http://bbs.csdn.net/forums/Java
  • 打赏
  • 举报
回复
引用 1 楼 xuzuning 的回复:
可以这样
s = '5201133';
if(s == s.replace(/(.)\1(.)\2/, function(m, a, b) { return parseInt(a)+1 == b ? m : ''; }))
  document.write('yes');
else
  document.write('no');

s = '5201122';
if(s == s.replace(/(.)\1(.)\2/, function(m, a, b) { return parseInt(a)+1 == b ? m : ''; }))
  document.write('yes');
else
  document.write('no');
我希望可以用正则。因为我主要写在JAVA程序里面。
幽狼 2013-06-24
  • 打赏
  • 举报
回复
^\d*(?:(1){2}(?<=\1)(2)\2|(2){2}(?<=\3)(3)\4|(3){2}(?<=\5)(4)\6|(4){2}(?<=\7)(5)\8|(5){2}(?<=\9)(6)\10|(6){2}(?<=\11)(7)\12|(7){2}(?<=\13)(8)\14|(8){2}(?<=\15)(9)\16)\d*$
xuzuning 2013-06-23
  • 打赏
  • 举报
回复
可以这样
s = '5201133';
if(s == s.replace(/(.)\1(.)\2/, function(m, a, b) { return parseInt(a)+1 == b ? m : ''; }))
  document.write('yes');
else
  document.write('no');

s = '5201122';
if(s == s.replace(/(.)\1(.)\2/, function(m, a, b) { return parseInt(a)+1 == b ? m : ''; }))
  document.write('yes');
else
  document.write('no');

10,607

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 其他
社区管理员
  • 其他
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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