Question: I'm error checking Canadian postal codes in the format A1A1A1.
Common typos are capital O instead of zeros in positions 2, 4 or 6, which should be replaced by a zero.

I'm fairly new to regex, and this one has me stumped.
Thanks so much!

Answer: 
You can do

```
x <- c("A0A0A0", "AOB0C0", "A0BOC0", "A0B0CO", "OOOOOO")

gsub("([A-Z])O", "\\10", x)
# [1] "A0A0A0" "A0B0C0" "A0B0C0" "A0B0C0" "O0O0O0"

```

A bit of explanation:

* `[A-Z]` is any character from `A` to `Z`
* the parentheses `([A-Z])` are here to capture the character so it can be referenced as `\\1` in the replacement
* `([A-Z])O` is a character from `A` to `Z` followed by a `O`
* `\\1` is the captured character from `A` to `Z`
* `\\10` is the captured character followed by a `0`
If the format is always like that, you could use `gsub` to replace the mistaken "O" characters.

```
x <- c('A1A1A1', 'AOAOAO', 'A0B0CO', 'AOBOC0')
gsub('[A-Z]\\KO', '0', x, perl=T)
# [1] "A1A1A1" "A0A0A0" "A0B0C0" "A0B0C0"

```