Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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".

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading