Find and replace with Regex groups
Create Regex groups with parentheses for powerful find/replace patterns.
Setup: I have a markdown file with a lot of shorthand links to GitHub PRs,
e.g. user/repo#1
, that I wanted to be replaced with full URLs:
https://github.com/user/repo/issues/1
.
These steps are for VS Code, but you can do a Regex find/replace in any environment.
Step by step:
- Open the search panel in VS Code
- Turn on Regex searches with the
.*
toggle - Search with the following Regex pattern:
([\w-]+)/(.+)#(\d+)
- This looks for any text that matches the pattern of:
([\w-]+)
a word containing one more alphanumeric or hyphen characters,/
a forward slash,(.+)
one or more of any character,#
a pound sign, and(\d+)
one or more digits
- It creates three groups (identified by the parentheses)
- This looks for any text that matches the pattern of:
- Replace the matched text with the following pattern:
https://github.com/$1/$2/issues/$3
Try it out:
Tradeoffs: The regex pattern above will only match for alphanumeric GitHub usernames with optional hyphens. I don’t know the official pattern used to validate usernames, so your data might include some that don’t match.