Skip to content

Instantly share code, notes, and snippets.

@Wind010
Last active October 8, 2020 17:52
Show Gist options
  • Select an option

  • Save Wind010/cd0bf37bcca611b0b8656ea1557212c9 to your computer and use it in GitHub Desktop.

Select an option

Save Wind010/cd0bf37bcca611b0b8656ea1557212c9 to your computer and use it in GitHub Desktop.
Snippet to search and replace in file via regex certain patterns.
# Already case-insensitive. Use -cmatch and -creplace for case-sensitive matching and replacement.
(Get-Content input.json) `
-replace '\d{15,19}', {'*' * ($_.Value.Length-4) + $_.Value.Substring($_.Value.Length - 5, 4)} `
-replace '(\"expirationMonth\":)(\s*)("\d{1,2}")', '$1$2"**"' `
-replace '(\"expirationYear\":)(\s*)("\d{2,4}")', '$1$2"****"' `
-replace '(\"cvn\":)(\s*)("\d{3,4}")', '$1$2"***"' |
Out-File output.json
# Where $1 is the first group for example "CVN": and $2 is the second group (zero or more spaces) and $3 is the value.
# Alternatively if we wanted to masking with groups
# -replace '(\"cvn\":)\s+"(\d{2})(\d{1,2})"', '$1 **$3'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment