Skip to content

Instantly share code, notes, and snippets.

@mvogelgesang
Created February 3, 2022 16:55
Show Gist options
  • Save mvogelgesang/f44d0c8980fd03f8b47c4719430f538e to your computer and use it in GitHub Desktop.
Save mvogelgesang/f44d0c8980fd03f8b47c4719430f538e to your computer and use it in GitHub Desktop.
Find and Replace with RegEx Matching Groups

Background

Had a pre-existing JSON file which was missing an attribute for website domain. Rather than manually editing each file, sought out a solution which could be applied across all files at once.

Input

{
  "scanVersion": "0.0.1",
  "url": "1.usa.gov/blah",
}

Output

{
  "scanVersion": "0.0.1",
  "domain": "1.usa.gov",
  "url": "1.usa.gov/blah",
}

Find/ Replace Criteria

Find "scanVersion": "0\.0\.1",\n.*("url":\s"(.*?)(\/.*?)?")

Replace

"scanVersion": "0.0.1",
"domain": "$2",
$1

Breakdown

Group 1: "url": "1.usa.gov/blah",

Group 2: 1.usa.gov

Group 3: /blah",

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment