Finance
Invoice number (INV-YYYY-NNNN) Regex
Many accounting systems number invoices with a prefix, the year and a zero-padded sequence, such as INV-2026-0042. This pattern matches that convention: the letters INV, a dash, a four digit year, another dash and a sequence of four to six digits. The i flag lets lowercase prefixes through, which helps when users type references into a search box. Use it to pick invoice references out of email subjects, validate a reference field or route payments to the right ledger. It is a template rather than a standard, so change the prefix and digit counts to suit your own numbering scheme.
Pattern (JavaScript)
/^INV-\d{4}-\d{4,6}$/iStarts at the start of the line/string, then the text "INV-", then exactly 4 digits, then the character "-", then a digit (0-9), between 4 and 6 times, then the end of the line/string.
Examples
Matches
- ✓ INV-2026-0001
- ✓ INV-2025-123456
- ✓ inv-2026-0042
Does not match
- ✗ INV2026-0001
- ✗ INV-26-0001
- ✗ INV-2026-001
- ✗ INV-2026-1234567
Try it
0 matches (JavaScript engine)
INV-2026-0001
INV-2025-123456
inv-2026-0042
INV2026-0001
INV-26-0001
INV-2026-001
INV-2026-1234567Code in 8 languages
Converted automatically from the JavaScript pattern; language-specific notes are shown where the syntax or support differs.
JavaScript
const re = /^INV-\d{4}-\d{4,6}$/gi;
const text = "INV-2026-0001";
const matches = [...text.matchAll(re)].map((m) => m[0]);
console.log(matches);Python
import re
pattern = re.compile(r'^INV-\d{4}-\d{4,6}$', re.I)
text = 'INV-2026-0001'
matches = [m.group(0) for m in pattern.finditer(text)]
print(matches)PHP
<?php
$pattern = '/^INV-\d{4}-\d{4,6}$/i';
$text = 'INV-2026-0001';
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("^INV-\\d{4}-\\d{4,6}$", Pattern.CASE_INSENSITIVE);
String text = "INV-2026-0001";
Matcher matcher = pattern.matcher(text);
while (matcher.find()) {
System.out.println(matcher.group());
}
}
}Go
package main
import (
"fmt"
"regexp"
)
func main() {
re := regexp.MustCompile(`(?i)^INV-\d{4}-\d{4,6}$`)
text := "INV-2026-0001"
fmt.Println(re.FindAllString(text, -1))
}C#
using System;
using System.Text.RegularExpressions;
var pattern = new Regex(@"^INV-\d{4}-\d{4,6}$", RegexOptions.IgnoreCase);
var text = "INV-2026-0001";
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"(?i)^INV-\d{4}-\d{4,6}$").unwrap();
let text = "INV-2026-0001";
for m in re.find_iter(text) {
println!("{}", m.as_str());
}
}- Add regex = "1" under [dependencies] in Cargo.toml.
Ruby
pattern = /^INV-\d{4}-\d{4,6}$/i
text = 'INV-2026-0001'
matches = text.to_enum(:scan, pattern).map { Regexp.last_match[0] }
p matchesFrequently asked questions
- Is there an official invoice number format?
- No. Most tax rules only require invoice numbers to be unique and sequential. The INV-YYYY-NNNN style is a common convention, so adapt this pattern to the scheme your accounting software uses.
- Why does the year part accept any four digits?
- Keeping it to \d{4} keeps the pattern simple and future-proof. If you want to block obviously wrong years, replace it with 20\d{2} to accept only years from 2000 to 2099.
- How do I find invoice numbers inside an email body?
- Replace the ^ and $ anchors with \b word boundaries and add the g flag. The pattern will then find every invoice reference in the text instead of testing the whole string.
Related patterns
- Accounting negative amount in parentheses^\((?:\d{1,3}(?:,\d{3})*|\d+)(?:\.\d{2})?\)$
- American Express card number shape^3[47]\d{2}[ -]?\d{6}[ -]?\d{5}$
- Bitcoin address shape^(?:[13][a-km-zA-HJ-NP-Z1-9]{25,34}|bc1[ac-hj-np-z02-9]{11,71})$
- Common ISO 4217 currency code^(?:USD|EUR|GBP|JPY|CHF|CAD|AUD|NZD|CNY|HKD|SGD|INR|AED|SAR|SEK|NOK|DKK|ZAR|BRL|MXN)$