Skip to content

Instantly share code, notes, and snippets.

@markhuge
Last active February 14, 2019 07:11
Show Gist options
  • Save markhuge/4724990 to your computer and use it in GitHub Desktop.
Save markhuge/4724990 to your computer and use it in GitHub Desktop.
Using regex capture groups in Sublime Text 2 search and replace

I copied my .csshrc to a new machine, and needed to add a domain suffix to the hostnames of 20+ VMs in my cluster config. All of my hostnames end in at least 2 digits (foo-bar-baz-01), so I:

  1. Search for instances of 2 digits followed by whitespace.
  2. Preserve the digits in a capture group.
  3. Append the suffix to the capture group (\1)

Append domain suffix where none exists:

Search: ([0-9]{2})\s Replace: \1.my.domain.suffix

Replace domain suffix:

Search: ([0-9]{2})\.my\.old\.domain Replace: \1.my.new.domain

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