^(0[oO])?[0-7]+$ This pattern matches an octal (base-8) number using digits 0 through 7, with an optional 0o or 0O prefix as used by modern languages. It validates octal literals such as Unix permission modes. Numeric patterns validate or extract numbers in a particular notation — integers, decimals, scientific notation, hex, binary or currency. The hard part is bounding ranges and forbidding malformed forms (leading zeros, stray signs, misplaced separators) that a naive \d+ would accept. Always pair a numeric regex with a real parse when magnitude, overflow or precision matter. It needs no special flags. Apply it to the whole input as a format contract. Legacy C-style octal uses a bare leading 0 (e.g. 0755); this pattern accepts that since the prefix is optional and digits are 0-7. It does not distinguish octal from decimal for values like 12, which are valid in both — context determines interpretation. Digits 8 and 9 are correctly rejected, which is the key difference from a decimal pattern. As with any regular expression, confirm its behavior in your target engine, test boundary and adversarial inputs, and watch for catastrophic backtracking before relying on it in production.
Token-by-token explanation
(0[oO])? — optional 0o or 0O prefix[0-7]+ — one or more octal digits (0-7)^...$ — anchors spanning the whole string
What it matches
7550o6440077712 What it does not match
80o97890x1F(empty string) When to use it
Reach for a numeric pattern when input must be a number in a specific notation, such as a quantity, a price, a coordinate or a bit string. Validate the textual shape first, then parse to a numeric type and check range, precision and overflow in code, because regex cannot reliably bound magnitude. Decide explicitly whether signs, leading zeros, separators and exponents are allowed, and document that contract so producers and consumers of the data agree on the accepted form.
Caveats
- Legacy C-style octal uses a bare leading 0 (e.g. 0755); this pattern accepts that since the prefix is optional and digits are 0-7.
- It does not distinguish octal from decimal for values like 12, which are valid in both — context determines interpretation.
- Digits 8 and 9 are correctly rejected, which is the key difference from a decimal pattern.
Code examples
const re = /^(0[oO])?[0-7]+$/;
const input = "755";
if (re.test(input)) {
// valid: proceed
} else {
// reject with a helpful message
} import re
rx = re.compile(r"^(0[oO])?[0-7]+$")
value = "755"
if rx.fullmatch(value):
pass # valid
else:
raise ValueError("invalid format") import java.util.regex.*;
Pattern p = Pattern.compile("^(0[oO])?[0-7]+$");
Matcher m = p.matcher("755");
boolean ok = m.matches(); // whole input must conform <?php
$subject = "755";
if (preg_match('/^(0[oO])?[0-7]+$/', $subject, $m)) {
// $m[0] is the full match
} How to test it
Before trusting any pattern, test it against three kinds of input: valid examples that must match, invalid examples that must be rejected, and edge cases at the boundaries (empty strings, maximum lengths, unusual separators and Unicode). Paste the expression into a live regex tester with your actual sample data and confirm the highlighted matches are exactly what you intend. Watch for catastrophic backtracking by trying long non-matching inputs, and remember that engine differences (JavaScript, Python, Java, PCRE, Go/RE2) mean a pattern that works in one may fail or behave differently in another — especially lookarounds, which Go's RE2 does not support at all.
Frequently asked questions
Why do Unix permissions use octal?
Each permission triplet (read/write/execute) maps to three bits, i.e. one octal digit 0-7, so a mode like 755 compactly encodes three sets of rwx permissions.
Is 0755 the same as 755 here?
Both match this pattern. The leading zero is the traditional octal marker; whether it is required depends on your language or convention.
Can a regex fully validate a number's range?
Only awkwardly. Digit classes accept out-of-range values (e.g. \d{1,3} allows 999), so precise bounds need alternation like 25[0-5]|2[0-4]\d. For most cases, match the shape then parse and range-check in code.
Why are leading zeros often rejected?
Leading zeros are ambiguous: 010 may mean decimal 10 or octal 8, and canonical numbers do not use them. Patterns like (0|[1-9]\d*) forbid them so the textual form maps unambiguously to a value.
Try the Regex Tester
Live regex matching with groups, flags and explanations.