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

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,}}"),"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\\}\\],}\\}"));

原文地址:https://www.jb51.cc/java/129539.html

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

相关推荐