1

I have the following JSON file. Dotted across the file is the following:

"properties": {
    "Name": "Darlington",
    "Description": "<br><br><br> <table border=\"1\" padding=\"0\"> <tr><td>CCGcode</td><td>00C</td></tr> <tr><td>CCGname_short</td><td>Darlington</td></tr>"
}

Using RegEx, I would like to extract the CCG Code property and add it back in so that the above becomes:

"properties": {
    "Name": "Darlington",
    "CCGcode": "00C",
    "Description": "<br><br><br> <table border=\"1\" padding=\"0\"> <tr><td>CCGcode</td><td>00C</td></tr> <tr><td>CCGname_short</td><td>Darlington</td></tr>"
}

I've tried all sorts and I just can't get it to work. I am using Sublime Text.

^("Description":").*?<td>CCGcode<\/td><td>([^<>\n]*).*$

The above selects the code, but not sure how I can get it to create the property.

halfer
  • 19,824
  • 17
  • 99
  • 186
J86
  • 14,345
  • 47
  • 130
  • 228

2 Answers2

2

There's a very simpel, but not so elegant, solution. Replace

"Description":.*?<td>CCGcode<\/td><td>([A-Z\d]*)<\/td>

with

"CCGCode":"\1",\n    \0

Don't know how Sublime handles replacements, but you may have to change the replacing \0 and \1 to something else - e.g. $0 and $1.

What it does is to find the Description entry and the following CCGCode entry, capturing the code into capture group 1.

Then replace capture group 0 - the whole matched text, with the new CCGCode JSON tag plus the original text.

It's a pretty fragile solution, but it works for your sample case.

Check out example at regex101.

Regards

SamWhan
  • 8,296
  • 1
  • 18
  • 45
  • [Check this question.](http://stackoverflow.com/questions/11819886/regular-expression-search-replace-in-sublime-text-2). Apparently bot `\1` and `$1` should work :) – SamWhan Apr 19 '16 at 08:37
  • This works too, but `(00C)` needed to be generalised so that it applied to all `CCGcodes`. +1 for explanation. Thank You – J86 Apr 19 '16 at 08:40
  • Of course, sloppiness from my side. That was the intention ;) Corrected the answer. – SamWhan Apr 19 '16 at 08:43
2

Try this

( *)"Description".*?CCGcode.*?<td>([^<]+)

Regex demo

This one for sublimetext3

Find what:

( *)("Description".*?CCGcode.*?<td>)([^<]+)

Replace with:

\1"CCGcode": "\3",\n\1\2

Demo

Tim007
  • 2,557
  • 1
  • 11
  • 20