【问题标题】:Android - Java Regex - Syntax error in regexp pattern near index [duplicate]Android - Java Regex - 索引附近的正则表达式模式中的语法错误[重复]
【发布时间】:2016-01-28 06:40:46
【问题描述】:

我的正则表达式有问题。在字符串中设置表达式时会出错。看这个!我想在数组中返回一个对象,该对象具有我的搜索值,例如“Daventry”。

我正在使用:{[^{}\]]*Daventry[^{}\]]*}

[
    {
        "id": 113548,
        "name": "Daventry",
        "state_id": 1446,
        "state_name": "Northamptonshire",
        "state_code": "J1",
        "country_id": 232,
        "country_name": "United Kingdom",
        "country_code": "GB",
        "country_flag": "GB.png",
        "currency_code": "GBP",
        "currency_symbol": "£"
    },
    {
        "id": 192392,
        "name": "Daverdisse",
        "state_id": 484,
        "state_name": "Luxembourg",
        "state_code": "06",
        "country_id": 22,
        "country_name": "Belgium\n",
        "country_code": "BE",
        "country_flag": "BE.png",
        "currency_code": "EUR",
        "currency_symbol": "€"
    },
    {
        "id": 115981,
        "name": "Davey",
        "state_id": 4138,
        "state_name": "Nebraska",
        "state_code": "NE",
        "country_id": 233,
        "country_name": "United States\n",
        "country_code": "US",
        "country_flag": "US.png",
        "currency_code": "USD",
        "currency_symbol": "$"
    },
    {
        "id": 142184,
        "name": "David",
        "state_id": 3350,
        "state_name": "Neamt",
        "state_code": "28",
        "country_id": 181,
        "country_name": "Romania\n",
        "country_code": "RO",
        "country_flag": "RO.png",
        "currency_code": "RON",
        "currency_symbol": "lei"
    },
    {
        "id": 108261,
        "name": "David",
        "state_id": 4127,
        "state_name": "Kentucky",
        "state_code": "KY",
        "country_id": 233,
        "country_name": "United States\n",
        "country_code": "US",
        "country_flag": "US.png",
        "currency_code": "USD",
        "currency_symbol": "$"
    }
]

并且只返回:

[
        {
            "id": 113548,
            "name": "Daventry",
            "state_id": 1446,
            "state_name": "Northamptonshire",
            "state_code": "J1",
            "country_id": 232,
            "country_name": "United Kingdom",
            "country_code": "GB",
            "country_flag": "GB.png",
            "currency_code": "GBP",
            "currency_symbol": "£"
        }
]

但是给出以下错误:

java.util.regex.PatternSyntaxException:正则表达式模式中的语法错误 索引 1 附近:10-27 09:53:48.297 15052-15052/com.plugapps.freecommerce E/AndroidRuntime: {[/^{}]]Daventry[/^{}]]}

我该怎么办?

【问题讨论】:

  • 不要尝试正则表达式,而是使用 Json 解析,您可以获得对象作为单独的数组。
  • @GIRIDHARANARI 想使用,因为我有一个包含 100,000 个对象的文件,但是在 Android 中的搜索非常慢。

标签: java android regex expression


【解决方案1】:

在正则表达式中,'{'、'['等是特殊字符,需要转义,所以你应该使用\{来表达{,所以你的refex表达式是错误的,所以会抛出这样的异常.
其实我不建议你使用正则表达式来搜索你的情况,你最好解析一个jsonarray并检查每个jsonobject的名称。

【讨论】:

  • 我再考一次。我想使用,因为我有一个包含 100,000 个对象的文件,但是在 Android 中的搜索非常慢。
猜你喜欢
  • 2013-09-06
  • 2012-11-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-10-28
  • 1970-01-01
相关资源
最近更新 更多