0

I need a little help with regular expressions. I need to locate a string:

{"users":[

and replace it with

{"name": "blaaaa", "users":[

In a complex json file.

I am completely ignorant about regexes.

Thank you

Srdjan Nikitovic
  • 853
  • 2
  • 9
  • 19
  • 4
    any attempts?.. – Avinash Raj Sep 16 '16 at 13:40
  • 4
    If you want to handle actual JSON cases - don't do it with regexes, use a parser. If you don't - there is nothing regexy about this case, simply replace `'"users"'` with `'"name": "blaaaa", "users"'`. – ndnenkov Sep 16 '16 at 13:44

1 Answers1

1

Using regex for JSON, or any other format that requires a parser, is not a good idea. Parse it using your favorite parser, add name attribute, and serialize the results back to JSON.

If you would like to continue with a regex exercise, however, here is how you can do it: one simple approach is to search for the { preceding "users", and then replace it with {"name":"blaaaa", text. The "followed by" concept is expressed with the look-ahead construct (?=<text>):

{(?="users":\[)

Note the escape symbol \ in front of [, which is necessary because [ is part of regex syntax.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523