Regex replace: удалить свойство с условием - PullRequest
1 голос
/ 06 ноября 2019

Я хотел бы использовать Regex replace для удаления определенного свойства из массива JSON с условием, что получают только элементы с

  • "name": "entity_to_catch" и "direction": 4неправдоподобным.
  • Остальные элементы, например, "name": "some_entity1", должны быть не тронуты.

Я пробовал несколько шаблонов Regex - также с предвкушением Regex - но ничего не получает нужные данные.

Это мой шаблон: (entity_to_catch)(.*?)("direction": 4,)([^}]*) с удалением группы 3 $1$2$4

Это мои данные:

{
    "id": 1,
    "name": "entity_to_catch",
    "position": {
        "x": -68,
        "y": -98
    },
    "type": "input"
},
{
    "id": 2,
    "name": "entity_to_catch",
    "position": {
        "x": -58,
        "y": -98
    },
    "type": "input"
},
{
    "id": 1248,
    "name": "some_entity1",
    "position": {
        "x": 64,
        "y": -41
    },
    "direction": 6
},
{
    "id": 1249,
    "name": "some_entity1",
    "position": {
        "x": 67,
        "y": -41
    }
},
{
    "id": 1250,
    "name": "entity_to_catch",
    "position": {
        "x": 67,
        "y": -43
    },
    "direction": 2,
    "type": "input"
},
{
    "id": 1251,
    "name": "some_entity1",
    "position": {
        "x": 70,
        "y": -41
    },
    "direction": 2
},
{
    "id": 1255,
    "name": "entity_to_catch",
    "position": {
        "x": 77,
        "y": -43
    },
    "type": "output"
},
{
    "id": 1256,
    "name": "some_entity1",
    "position": {
        "x": 80,
        "y": -41
    },
    "direction": 2
},
{
    "id": 1364,
    "name": "some_entity1",
    "position": {
        "x": 65,
        "y": -38
    },
    "direction": 2
},
{
    "id": 1365,
    "name": "entity_to_catch",
    "position": {
        "x": 67,
        "y": -38
    },
    "direction": 4,
    "type": "output"
},
{
    "id": 1366,
    "name": "some_entity1",
    "position": {
        "x": 69,
        "y": -38
    },
    "direction": 6
},
{
    "id": 1369,
    "name": "some_entity1",
    "position": {
        "x": 75,
        "y": -38
    },
    "direction": 2
},
{
    "id": 1370,
    "name": "entity_to_catch",
    "position": {
        "x": 77,
        "y": -38
    },
    "type": "input"
},
{
    "id": 1668,
    "name": "entity_to_catch",
    "position": {
        "x": -48,
        "y": -29
    },
    "type": "input"
},
{
    "id": 1669,
    "name": "entity_to_catch",
    "position": {
        "x": -38,
        "y": -28
    },
    "direction": 4,
    "type": "input"
},

Данные после замены должны выглядеть следующим образом:

{
    "id": 1,
    "name": "entity_to_catch",
    "position": {
        "x": -68,
        "y": -98
    },
    "type": "input"
},
{
    "id": 2,
    "name": "entity_to_catch",
    "position": {
        "x": -58,
        "y": -98
    },
    "type": "input"
},
{
    "id": 1248,
    "name": "some_entity1",
    "position": {
        "x": 64,
        "y": -41
    },
    "direction": 6
},
{
    "id": 1249,
    "name": "some_entity1",
    "position": {
        "x": 67,
        "y": -41
    }
},
{
    "id": 1250,
    "name": "entity_to_catch",
    "position": {
        "x": 67,
        "y": -43
    },
    "direction": 2,
    "type": "input"
},
{
    "id": 1251,
    "name": "some_entity1",
    "position": {
        "x": 70,
        "y": -41
    },
    "direction": 2
},
{
    "id": 1255,
    "name": "entity_to_catch",
    "position": {
        "x": 77,
        "y": -43
    },
    "type": "output"
},
{
    "id": 1256,
    "name": "some_entity1",
    "position": {
        "x": 80,
        "y": -41
    },
    "direction": 2
},
{
    "id": 1364,
    "name": "some_entity1",
    "position": {
        "x": 65,
        "y": -38
    },
    "direction": 2
},
{
    "id": 1365,
    "name": "entity_to_catch",
    "position": {
        "x": 67,
        "y": -38
    },
    "type": "output"
},
{
    "id": 1366,
    "name": "some_entity1",
    "position": {
        "x": 69,
        "y": -38
    },
    "direction": 6
},
{
    "id": 1369,
    "name": "some_entity1",
    "position": {
        "x": 75,
        "y": -38
    },
    "direction": 2
},
{
    "id": 1370,
    "name": "entity_to_catch",
    "position": {
        "x": 77,
        "y": -38
    },
    "type": "input"
},
{
    "id": 1668,
    "name": "entity_to_catch",
    "position": {
        "x": -48,
        "y": -29
    },
    "type": "input"
},
{
    "id": 1669,
    "name": "entity_to_catch",
    "position": {
        "x": -38,
        "y": -28
    },
    "type": "input"
},
...