Kallen
Kallen

Reputation: 13

Regex to find all quotes between square brackets

Seems pretty simple but I can't find anything that works. Just want to capture all double quotes between square brackets so I can replace them with an empty string. The values between the square brackets are always between [0-9].

Thanks for your help.

Input

    "someText"=["1111","2222"]

Output

    "someText"=[1111,2222]

Upvotes: 1

Views: 346

Answers (2)

Armali
Armali

Reputation: 19375

In my case array elements will always be integers just wrapped in quotes. No special characters will be between the quotes, just [0-9].

Since you have no nested brackets, you can lookahead of a quote for a closing bracket without an intervening opening bracket:

    public static void main(String[] args)
    {
        System.out.println(args[0].replaceAll("\"(?=[^\\[]*])", ""));
    }

Upvotes: 0

rzwitserloot
rzwitserloot

Reputation: 102822

Regular Expressions are a tool that works when the input is regular. That is a specific bit of jargon, with a very specific meaning.

So, if the input you have is not regular, a regular expression cannot parse this. This format looks like JSON. And here's the bad news: JSON is not regular :( You just can't parse it with regexes. You write me a regexp that seems to work, I'll make you some valid JSON that your regexp replacer will mess up on.

You could fix it by adding bizarre caveats to what JSON can be input into your convoluted regex, but it seems like a much, much better idea to add a JSON parser to your code and use that.

Upvotes: 1

Related Questions