Home > other >  Remove all places that starts with known part till some known end part
Remove all places that starts with known part till some known end part

Time:01-16

I have some text (JSON) where I need to remove all parts that starts with known text and ends with some known text. Please help me build a 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"

I 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"
}

CodePudding user response:

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.

  • Related