加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

java – 具有“{”的字符串的模式匹配

发布时间:2020-12-15 00:42:46 所属栏目:Java 来源:网络整理
导读:首先我这样做了 – String str = "{"hits":[{"links_count":6,"forum_count":11}],"totalHitCount":1}"; Assert.assertTrue(str.matches("{"hits":[{"links_count":[0-9]{1,},"forum_count ":11}],"totalHitCount":[0-9]{1,}}"),"Partnershi
首先我这样做了 –
String str = "{"hits":[{"links_count":6,"forum_count":11}],"totalHitCount":1}";

        Assert.assertTrue(str.matches("{"hits":[{"links_count":[0-9]{1,},"forum_count   ":11}],"totalHitCount":[0-9]{1,}}"),"Partnership message does not appear");

这让我跟着错误 –

Exception in thread "main" java.util.regex.PatternSyntaxException: Illegal repetition
{"hits":[{"links_count":[0-9]{1,"forum_count":11}],"totalHitCount":[0-9]{1,}}

然后我做了(逃脱“{”) –

String str = "{"hits":[{"links_count":6,"forum_count":11}],"totalHitCount":1}";

    Assert.assertTrue(str.matches("{"hits":[{"links_count":[0-9]{1,}}"),"Partnership message does not appear");

并得到以下错误 –

Exception in thread "main" java.lang.AssertionError: Partnership message does not appear expected:<true> but was:<false>

我在这里想念的是什么?

解决方法

你不需要逃脱{[在你的输入中.但是你需要在你的正则表达式中逃避[].

试试这个:

String str = "{"hits":[{"links_count":6,"totalHitCount":1}";

System.out.println(str.matches("{"hits":[{"links_count":[0-9]{1,"forum_count":11}],}}"));

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读