Date/Time
Duration like 1h30m Regex
Command line tools, timers and configuration files often express durations compactly, such as 1h30m, 45m or 2h 15m 10s. This pattern accepts any combination of hours, minutes and seconds in that order, each a number followed by h, m or s, with optional single spaces between parts. At least one unit is required, so an empty string does not match. It is useful for timeout settings, time tracking inputs and chat bot commands. The honest limit is that decimal values like 1.5h, days or weeks, and unit words such as minutes are not supported.
Pattern (JavaScript)
/^(?:[0-9]+h(?: ?[0-9]+m)?(?: ?[0-9]+s)?|[0-9]+m(?: ?[0-9]+s)?|[0-9]+s)$/Starts at the start of the line/string, then (a digit, one or more times, then the character "h", then (a space, optionally (zero or one time), then a digit, one or more times, then the character "m"), optionally (zero or one time), then (a space, optionally (zero or one time), then a digit, one or more times, then the character "s"), optionally (zero or one time) OR a digit, one or more times, then the character "m", then (a space, optionally (zero or one time), then a digit, one or more times, then the character "s"), optionally (zero or one time) OR a digit, one or more times, then the character "s"), then the end of the line/string.
Examples
Matches
- ✓ 1h30m
- ✓ 45m
- ✓ 2h 15m 10s
- ✓ 90s
Does not match
- ✗ 1h30
- ✗ h30m
- ✗ 1.5h
- ✗ 30 minutes
Try it
0 matches (JavaScript engine)
1h30m
45m
2h 15m 10s
90s
1h30
h30m
1.5h
30 minutesCode in 8 languages
Converted automatically from the JavaScript pattern; language-specific notes are shown where the syntax or support differs.
JavaScript
const re = /^(?:[0-9]+h(?: ?[0-9]+m)?(?: ?[0-9]+s)?|[0-9]+m(?: ?[0-9]+s)?|[0-9]+s)$/g;
const text = "1h30m";
const matches = [...text.matchAll(re)].map((m) => m[0]);
console.log(matches);Python
import re
pattern = re.compile(r'^(?:[0-9]+h(?: ?[0-9]+m)?(?: ?[0-9]+s)?|[0-9]+m(?: ?[0-9]+s)?|[0-9]+s)$')
text = '1h30m'
matches = [m.group(0) for m in pattern.finditer(text)]
print(matches)PHP
<?php
$pattern = '/^(?:[0-9]+h(?: ?[0-9]+m)?(?: ?[0-9]+s)?|[0-9]+m(?: ?[0-9]+s)?|[0-9]+s)$/';
$text = '1h30m';
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("^(?:[0-9]+h(?: ?[0-9]+m)?(?: ?[0-9]+s)?|[0-9]+m(?: ?[0-9]+s)?|[0-9]+s)$");
String text = "1h30m";
Matcher matcher = pattern.matcher(text);
while (matcher.find()) {
System.out.println(matcher.group());
}
}
}Go
package main
import (
"fmt"
"regexp"
)
func main() {
re := regexp.MustCompile(`^(?:[0-9]+h(?: ?[0-9]+m)?(?: ?[0-9]+s)?|[0-9]+m(?: ?[0-9]+s)?|[0-9]+s)$`)
text := "1h30m"
fmt.Println(re.FindAllString(text, -1))
}C#
using System;
using System.Text.RegularExpressions;
var pattern = new Regex(@"^(?:[0-9]+h(?: ?[0-9]+m)?(?: ?[0-9]+s)?|[0-9]+m(?: ?[0-9]+s)?|[0-9]+s)$");
var text = "1h30m";
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"^(?:[0-9]+h(?: ?[0-9]+m)?(?: ?[0-9]+s)?|[0-9]+m(?: ?[0-9]+s)?|[0-9]+s)$").unwrap();
let text = "1h30m";
for m in re.find_iter(text) {
println!("{}", m.as_str());
}
}- Add regex = "1" under [dependencies] in Cargo.toml.
Ruby
pattern = /^(?:[0-9]+h(?: ?[0-9]+m)?(?: ?[0-9]+s)?|[0-9]+m(?: ?[0-9]+s)?|[0-9]+s)$/
text = '1h30m'
matches = text.to_enum(:scan, pattern).map { Regexp.last_match[0] }
p matchesFrequently asked questions
- Why must units appear in hour, minute, second order?
- A fixed order keeps the pattern simple and avoids ambiguous input such as 10m2h. Most tools that use this notation, such as Go's time.ParseDuration, write units from largest to smallest.
- Can minutes exceed 59, like 90m?
- Yes, the pattern accepts any number of minutes or seconds. If you want 1h30m instead of 90m, check the captured values in code after matching.
- How do I add days?
- Add an optional [0-9]+d part in front of the hours and allow it as its own alternative. Keep the same ordering rule so input remains predictable.