Identity
Canada SIN Shape Regex
A Canadian Social Insurance Number has nine digits and is often written in three groups of three, such as 123-456-789. The first digit indicates the issuing region or type, and 0 and 8 are not used, so this pattern rejects those. It accepts spaces, dashes or no separator between groups, which matches how Canadians usually type the number in payroll, banking and tax forms. The limit is the checksum: real SINs satisfy the Luhn algorithm, which a regular expression cannot evaluate, so validate that separately in application code.
Pattern (JavaScript)
/^[1-79][0-9]{2}[ -]?[0-9]{3}[ -]?[0-9]{3}$/Starts at the start of the line/string, then one of the characters 1-79, then a digit, exactly 2 times, then one of the characters -, optionally (zero or one time), then a digit, exactly 3 times, then one of the characters -, optionally (zero or one time), then a digit, exactly 3 times, then the end of the line/string.
Limit: Checks the shape only; it cannot tell whether a document is real or valid, and it does not run the Luhn checksum.
Examples
Matches
- ✓ 123-456-789
- ✓ 456 789 123
- ✓ 987654321
Does not match
- ✗ 012-345-678
- ✗ 823-456-789
- ✗ 123-45-6789
Try it
0 matches (JavaScript engine)
123-456-789
456 789 123
987654321
012-345-678
823-456-789
123-45-6789Code in 8 languages
Converted automatically from the JavaScript pattern; language-specific notes are shown where the syntax or support differs.
JavaScript
const re = /^[1-79][0-9]{2}[ -]?[0-9]{3}[ -]?[0-9]{3}$/g;
const text = "123-456-789";
const matches = [...text.matchAll(re)].map((m) => m[0]);
console.log(matches);Python
import re
pattern = re.compile(r'^[1-79][0-9]{2}[ -]?[0-9]{3}[ -]?[0-9]{3}$')
text = '123-456-789'
matches = [m.group(0) for m in pattern.finditer(text)]
print(matches)PHP
<?php
$pattern = '/^[1-79][0-9]{2}[ -]?[0-9]{3}[ -]?[0-9]{3}$/';
$text = '123-456-789';
preg_match_all($pattern, $text, $matches);
print_r($matches[0]);Java
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main {
public static void main(String[] args) {
Pattern pattern = Pattern.compile("^[1-79][0-9]{2}[ -]?[0-9]{3}[ -]?[0-9]{3}$");
String text = "123-456-789";
Matcher matcher = pattern.matcher(text);
while (matcher.find()) {
System.out.println(matcher.group());
}
}
}Go
package main
import (
"fmt"
"regexp"
)
func main() {
re := regexp.MustCompile(`^[1-79][0-9]{2}[ -]?[0-9]{3}[ -]?[0-9]{3}$`)
text := "123-456-789"
fmt.Println(re.FindAllString(text, -1))
}C#
using System;
using System.Text.RegularExpressions;
var pattern = new Regex(@"^[1-79][0-9]{2}[ -]?[0-9]{3}[ -]?[0-9]{3}$");
var text = "123-456-789";
foreach (Match m in pattern.Matches(text))
{
Console.WriteLine(m.Value);
}Rust
// Cargo.toml: regex = "1"
use regex::Regex;
fn main() {
let re = Regex::new(r"^[1-79][0-9]{2}[ -]?[0-9]{3}[ -]?[0-9]{3}$").unwrap();
let text = "123-456-789";
for m in re.find_iter(text) {
println!("{}", m.as_str());
}
}- Add regex = "1" under [dependencies] in Cargo.toml.
Ruby
pattern = /^[1-79][0-9]{2}[ -]?[0-9]{3}[ -]?[0-9]{3}$/
text = '123-456-789'
matches = text.to_enum(:scan, pattern).map { Regexp.last_match[0] }
p matchesFrequently asked questions
- What does a SIN starting with 9 mean?
- A first digit of 9 is used for temporary residents such as international students and workers. The pattern accepts it, but your business rules may need to treat those numbers differently.
- Does the pattern require consistent separators?
- No, each separator is independent, so 123-456 789 also matches. If you need consistency, capture the first separator and use a backreference, or normalise the input before testing.
- How do I check the Luhn digit?
- Strip the separators, double every second digit from the left starting with the second, subtract 9 from any result above 9, and check that the total is divisible by 10. This must be done in code after the regex matches.