/ regex

Find and replace RegEx variables in Anki

I just wanted to put this out there for anyone struggling to replace strings in Anki using dynamically grouped variables in their regular expressions (RegEx). The difference between normal RegEx variables and Anki RegEx variables is that the dollar sign $ becomes a backslash \.

For example:

You have a 10-digit phone number in one of your Anki card fields:
"Bob's phone number is 5555555555"

You want to add hyphens to the ten digit number so, using RegEx, you make three groupings using parentheses in the find field:

Find: ([0-9]{3})([0-9]{3})([0-9]{4})

The regular expression above matches any set of 10 digits and divides them into 3 digits, 3 digits again, and then 4 digits (like you would split them in a phone number). Groupings in RegEx are defined with parentheses. Since there are three groupings in our RegEx above, we can call our groupings using the variables: \1, \2, and \3.

As far as I know you can have as many RegEx groupings as you like (\1, \2, \3, ...\12, ...\39, etc.).

And to add hyphens between the groups, our replace statement will look like this:

Replace: \1-\2-\3

We should successfully have an output of:
"Bob's phone number is 555-555-5555"

I found this out reading the example in the docs, but it wasn't immediately obvious. And so, I thought it would avoid other people hassle to have another reference they could find and decided to note it here on my blog. I hope it helps Anki users who are new to RegEx as well as those familiar with it. That's it!