Finance
Accounting negative amount in parentheses Regex
Financial statements often show negative numbers inside parentheses instead of with a minus sign, so a loss of 1,234.56 appears as (1,234.56). This pattern matches that convention: an opening parenthesis, a number with optional comma grouping and optional two digit decimals, and a closing parenthesis. It is useful when importing spreadsheets or PDF tables into a system that expects a minus sign, because you can detect these cells and convert them. It does not allow currency symbols inside the parentheses, so strip those first, and it cannot tell a negative amount from a parenthetical note that happens to contain a number.
Pattern (JavaScript)
/^\((?:\d{1,3}(?:,\d{3})*|\d+)(?:\.\d{2})?\)$/Starts at the start of the line/string, then the character "(", then (a digit (0-9), between 1 and 3 times, then (the character ",", then exactly 3 digits), zero or more times OR one or more digits), then (the character ".", then exactly 2 digits), optionally (zero or one time), then the character ")", then the end of the line/string.
Examples
Matches
- ✓ (1,234.56)
- ✓ (500)
- ✓ (0.75)
- ✓ (1000000.00)
Does not match
- ✗ -1,234.56
- ✗ (1,234.56
- ✗ (12,34)
- ✗ (abc)
Try it
0 matches (JavaScript engine)
(1,234.56)
(500)
(0.75)
(1000000.00)
-1,234.56
(1,234.56
(12,34)
(abc)Code in 8 languages
Converted automatically from the JavaScript pattern; language-specific notes are shown where the syntax or support differs.
JavaScript
const re = /^\((?:\d{1,3}(?:,\d{3})*|\d+)(?:\.\d{2})?\)$/g;
const text = "(1,234.56)";
const matches = [...text.matchAll(re)].map((m) => m[0]);
console.log(matches);Python
import re
pattern = re.compile(r'^\((?:\d{1,3}(?:,\d{3})*|\d+)(?:\.\d{2})?\)$')
text = '(1,234.56)'
matches = [m.group(0) for m in pattern.finditer(text)]
print(matches)PHP
<?php
$pattern = '/^\((?:\d{1,3}(?:,\d{3})*|\d+)(?:\.\d{2})?\)$/';
$text = '(1,234.56)';
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("^\\((?:\\d{1,3}(?:,\\d{3})*|\\d+)(?:\\.\\d{2})?\\)$");
String text = "(1,234.56)";
Matcher matcher = pattern.matcher(text);
while (matcher.find()) {
System.out.println(matcher.group());
}
}
}Go
package main
import (
"fmt"
"regexp"
)
func main() {
re := regexp.MustCompile(`^\((?:\d{1,3}(?:,\d{3})*|\d+)(?:\.\d{2})?\)$`)
text := "(1,234.56)"
fmt.Println(re.FindAllString(text, -1))
}C#
using System;
using System.Text.RegularExpressions;
var pattern = new Regex(@"^\((?:\d{1,3}(?:,\d{3})*|\d+)(?:\.\d{2})?\)$");
var text = "(1,234.56)";
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"^\((?:\d{1,3}(?:,\d{3})*|\d+)(?:\.\d{2})?\)$").unwrap();
let text = "(1,234.56)";
for m in re.find_iter(text) {
println!("{}", m.as_str());
}
}- Add regex = "1" under [dependencies] in Cargo.toml.
Ruby
pattern = /^\((?:\d{1,3}(?:,\d{3})*|\d+)(?:\.\d{2})?\)$/
text = '(1,234.56)'
matches = text.to_enum(:scan, pattern).map { Regexp.last_match[0] }
p matchesFrequently asked questions
- Why do accountants use parentheses for negatives?
- Parentheses are easier to spot in long columns than a small minus sign and are harder to lose when printing. The convention is standard in balance sheets and income statements.
- How do I convert a matched value to a negative number?
- Capture the inner number, remove the commas, parse it as a float and multiply by minus one. Wrapping the number part in a capture group makes that one simple step in code.
- Can I allow a currency symbol like ($1,234.56)?
- Yes. Add an optional symbol right after the opening parenthesis, for example \(\$?. Keep the symbol optional so plain parenthesised amounts continue to match.
Related patterns
- Invoice number (INV-YYYY-NNNN)^INV-\d{4}-\d{4,6}$
- 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)$