Regex – remove all places that starts with known part till some known end part

I have some text (JSON) for example,
that I need to remove all parts that starts with known text and ends with some known text.
Please help me do build regex to remove all that.

String input, part from JSON:

"oge": "GOF",
            "original": {
              "report": true,
              "tier": "IA"
            },
            "pertinentNegative": false,
            "populationFrequency": {
              "externalLinks": {
                "7-2-A-T": "https://x.d.org/s/7-d-A-T?dataset=s"
              },
              "maxPop": "South Asian",
              "maxPopAC": 1,
              "maxPopAN": 30,
              "maxPopFreq": 3.276,
              "overallPopAC": 1,
              "overallPopAN": 23,
              "overallPopFreq": 4.22,
              "source": "gnomAD"
            }

Known start part : "externalLinks"

Known end part : "maxPop"

Need to remove all that starts with "externalLinks" till "maxPop".

Output after regex work:

"oge": "GOF",
            "original": {
              "report": true,
              "tier": "IA"
            },
            "pertinentNegative": false,
            "populationFrequency": {
              "maxPop": "South Asian",
              "maxPopAC": 1,
              "maxPopAN": 30,
              "maxPopFreq": 3.276,
              "overallPopAC": 1,
              "overallPopAN": 23,
              "overallPopFreq": 4.22,
              "source": "gnomAD"
            }

Thank you !

>Solution :

Use the "replaceAll" method with the following pattern:

\"externalLinks\"[\s\S]+?(?=\"maxPop\")

This will match you every occurrence of "externalLinks" followed by any character, till next occurrence of ""maxPop"" (not included in the match). The lazy operator (?) will allow you to match the least amount possible of characters. Replace every match with the empty string.

Check the demo here.

Leave a Reply