Date/Time
Quarter and Year (Q1 2026) Regex
Business reports, earnings releases and project plans refer to periods as quarters, written like Q1 2026, Q4-2025 or occasionally Q32026. This pattern finds those labels in text, accepting quarters 1 to 4 and years from 1900 to 2099 with an optional space or dash between them, and captures the quarter number and year separately. It is useful for tagging documents, building report filters and pulling periods out of spreadsheet headers. The honest limit is that it cannot tell whether a label refers to a calendar quarter or a company's fiscal quarter, which may start in a different month.
Pattern (JavaScript)
/\bQ([1-4])[ -]?((?:19|20)[0-9]{2})\b/Matches a word boundary, then the character "Q", then a group (one of the characters 1-4), then one of the characters -, optionally (zero or one time), then a group ((the text "19" OR the text "20"), then a digit, exactly 2 times), then a word boundary.
Examples
Matches
- ✓ Q1 2026
- ✓ results for Q4-2025
- ✓ Q32026
Does not match
- ✗ Q5 2026
- ✗ Q1 26
- ✗ Q0 2026
Try it
3 matches (JavaScript engine)
Q1 2026 results for Q4-2025 Q32026 Q5 2026 Q1 26 Q0 2026
Code in 8 languages
Converted automatically from the JavaScript pattern; language-specific notes are shown where the syntax or support differs.
JavaScript
const re = /\bQ([1-4])[ -]?((?:19|20)[0-9]{2})\b/g;
const text = "Q1 2026";
const matches = [...text.matchAll(re)].map((m) => m[0]);
console.log(matches);Python
import re
pattern = re.compile(r'\bQ([1-4])[ -]?((?:19|20)[0-9]{2})\b')
text = 'Q1 2026'
matches = [m.group(0) for m in pattern.finditer(text)]
print(matches)PHP
<?php
$pattern = '/\bQ([1-4])[ -]?((?:19|20)[0-9]{2})\b/';
$text = 'Q1 2026';
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("\\bQ([1-4])[ -]?((?:19|20)[0-9]{2})\\b");
String text = "Q1 2026";
Matcher matcher = pattern.matcher(text);
while (matcher.find()) {
System.out.println(matcher.group());
}
}
}Go
package main
import (
"fmt"
"regexp"
)
func main() {
re := regexp.MustCompile(`\bQ([1-4])[ -]?((?:19|20)[0-9]{2})\b`)
text := "Q1 2026"
fmt.Println(re.FindAllString(text, -1))
}C#
using System;
using System.Text.RegularExpressions;
var pattern = new Regex(@"\bQ([1-4])[ -]?((?:19|20)[0-9]{2})\b");
var text = "Q1 2026";
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"\bQ([1-4])[ -]?((?:19|20)[0-9]{2})\b").unwrap();
let text = "Q1 2026";
for m in re.find_iter(text) {
println!("{}", m.as_str());
}
}- Add regex = "1" under [dependencies] in Cargo.toml.
Ruby
pattern = /\bQ([1-4])[ -]?((?:19|20)[0-9]{2})\b/
text = 'Q1 2026'
matches = text.to_enum(:scan, pattern).map { Regexp.last_match[0] }
p matchesFrequently asked questions
- How do I accept two-digit years like Q1 26?
- Change the year group to ((?:19|20)?[0-9]{2}) so the century becomes optional. Then decide in code which century a two-digit year belongs to.
- Does it match formats like 2026 Q1 or 2026-Q1?
- No, it expects the quarter first. Add a second alternative with the year first, for example \b((?:19|20)[0-9]{2})[ -]?Q([1-4])\b.
- How do I turn a quarter into a date range?
- For calendar quarters, the start month is (quarter - 1) * 3 + 1, and the quarter ends two months later. Fiscal quarters need your organisation's year start month instead.
Related patterns
- 12-Hour Time with AM/PM^(0?[1-9]|1[0-2]):[0-5][0-9] ?[AaPp][Mm]$
- 24-Hour Time (HH:MM)^([01][0-9]|2[0-3]):([0-5][0-9])$
- Date DD/MM/YYYY^(0[1-9]|[12][0-9]|3[01])[/.-](0[1-9]|1[0-2])[/.-]([0-9]{4})$
- Date with Month Name^(0?[1-9]|[12][0-9]|3[01]) (?:Jan(?:uary)?|Feb(?:ruary)?|Mar(?:ch)?|Apr(?:il)?|May|June?|July?|Aug(?:ust)?|Sep(?:t|tember)?|Oct(?:ober)?|Nov(?:ember)?|Dec(?:ember)?),? ([0-9]{4})$