微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

简单的正则表达式

一直在想着如何能写出一个正则表达式来判断一个字符串是否为json数组,想来想去没想出办法,先进行第一步,判断其是被{}或[]包裹吧,测试代码如下:

Pattern pattern = Pattern.compile("(^\\[.*\\]$|^\\{.*\\}$){1}");
 String s1 = "[aaa]"; //true
 String s2 = "[aaa";
 String s3 = "aaa]";
 String s4 = "{aaa}";//true
 String s5 = "{aaa";
 String s6 = "aaa}";
 String s7 = "aaa";
 System.out.println(pattern.matcher(s1).matches());
 System.out.println(pattern.matcher(s2).matches());
 System.out.println(pattern.matcher(s3).matches());
 System.out.println(pattern.matcher(s4).matches());
 System.out.println(pattern.matcher(s5).matches());
 System.out.println(pattern.matcher(s6).matches());
 System.out.println(pattern.matcher(s7).matches());

希望尽快找到办法.

原文地址:https://www.jb51.cc/regex/362602.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐