0

I have a problem in the regular expression. It is giving an error when the expression set in the string.

java.util.regex.PatternSyntaxException: Syntax error in regexp pattern near index 1: 10-27 09:53:48.297 15052-15052/com.plugapps.freecommerce E/AndroidRuntime: {[/^{}\]]*David[/^{}\]]*}

What do I do?

5
  • CAn you tell us what you are trying to do? Commented Oct 27, 2015 at 12:36
  • is {[/^{}]]David[/^{}]]} your regex? Commented Oct 27, 2015 at 12:36
  • @KevinEsche This is the expression : {[^{}]]*David[^{}]]*} Commented Oct 27, 2015 at 12:40
  • I guess you still have to escape the "\"s Commented Oct 27, 2015 at 12:44
  • And probably escape the leading and trailing '{' and '}' because {} is a repetition count (e.g. "\\{[/^{}\]]*David[/^{}\]]*\\}" Commented Oct 27, 2015 at 13:56

2 Answers 2

1

Look at this! I want returns an object in the Array that has the value of my search, for example "Daventry."

I am using: {[^{}\]]*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": "$"
    }
]

And only returns:

[
        {
            "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": "£"
        }
]

However gives the following error:

java.util.regex.PatternSyntaxException: Syntax error in regexp pattern near index 1: 10-27 09:53:48.297 15052-15052/com.plugapps.freecommerce E/AndroidRuntime: {[/^{}]]Daventry[/^{}]]}

Sign up to request clarification or add additional context in comments.

Comments

0

Your syntax is not completely right you still need to escape "/". If you change it to {[\/^{}\]]*David[\/^{}\]]*} then it's valid regex and it should work.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.