Contact
Fax Line with Label Regex
Business cards, letterheads and email signatures often list a fax number on its own line, labelled Fax or simply F. This pattern matches such a line from start to end, allowing an optional colon or dot after the label, an optional plus sign and digits mixed with spaces, dots, dashes and parentheses. It helps when you parse scanned contact blocks or clean up imported address books and need to keep fax numbers apart from voice lines. The honest limit is that it is loose about digit grouping and will accept oddly punctuated numbers.
Pattern (JavaScript)
/^(?:fax|f)\s*[:.]?\s*\+?[0-9(][0-9 ().-]{6,}[0-9]$/iStarts at the start of the line/string, then (the text "fax" OR the character "f"), then a whitespace character (space, tab, newline), zero or more times, then one of the characters :., optionally (zero or one time), then a whitespace character (space, tab, newline), zero or more times, then a literal "+", optionally (zero or one time), then one of the characters 0-9(, then one of the characters 0-9 ().-, 6 or more times, then a digit, then the end of the line/string.
Examples
Matches
- ✓ Fax: +1 415 555 0100
- ✓ fax 020 7946 0018
- ✓ F: (415) 555-0100
Does not match
- ✗ Phone: 415 555 0100
- ✗ Fax: none
- ✗ fax: 12
Try it
0 matches (JavaScript engine)
Fax: +1 415 555 0100
fax 020 7946 0018
F: (415) 555-0100
Phone: 415 555 0100
Fax: none
fax: 12Code in 8 languages
Converted automatically from the JavaScript pattern; language-specific notes are shown where the syntax or support differs.
JavaScript
const re = /^(?:fax|f)\s*[:.]?\s*\+?[0-9(][0-9 ().-]{6,}[0-9]$/gi;
const text = "Fax: +1 415 555 0100";
const matches = [...text.matchAll(re)].map((m) => m[0]);
console.log(matches);Python
import re
pattern = re.compile(r'^(?:fax|f)\s*[:.]?\s*\+?[0-9(][0-9 ().-]{6,}[0-9]$', re.I)
text = 'Fax: +1 415 555 0100'
matches = [m.group(0) for m in pattern.finditer(text)]
print(matches)PHP
<?php
$pattern = '/^(?:fax|f)\s*[:.]?\s*\+?[0-9(][0-9 ().-]{6,}[0-9]$/i';
$text = 'Fax: +1 415 555 0100';
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("^(?:fax|f)\\s*[:.]?\\s*\\+?[0-9(][0-9 ().-]{6,}[0-9]$", Pattern.CASE_INSENSITIVE);
String text = "Fax: +1 415 555 0100";
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)^(?:fax|f)\s*[:.]?\s*\+?[0-9(][0-9 ().-]{6,}[0-9]$`)
text := "Fax: +1 415 555 0100"
fmt.Println(re.FindAllString(text, -1))
}C#
using System;
using System.Text.RegularExpressions;
var pattern = new Regex(@"^(?:fax|f)\s*[:.]?\s*\+?[0-9(][0-9 ().-]{6,}[0-9]$", RegexOptions.IgnoreCase);
var text = "Fax: +1 415 555 0100";
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)^(?:fax|f)\s*[:.]?\s*\+?[0-9(][0-9 ().-]{6,}[0-9]$").unwrap();
let text = "Fax: +1 415 555 0100";
for m in re.find_iter(text) {
println!("{}", m.as_str());
}
}- Add regex = "1" under [dependencies] in Cargo.toml.
Ruby
pattern = /^(?:fax|f)\s*[:.]?\s*\+?[0-9(][0-9 ().-]{6,}[0-9]$/i
text = 'Fax: +1 415 555 0100'
matches = text.to_enum(:scan, pattern).map { Regexp.last_match[0] }
p matchesFrequently asked questions
- How do I pull out only the number?
- Wrap the part after the label in a capturing group, starting at \+? and ending at the final [0-9], then read that group. You can strip the punctuation afterwards to get plain digits.
- Why does the pattern require at least eight characters after the label?
- The number part needs a starting character, six or more middle characters and a final digit. This rejects stray labels followed by a short code that cannot be a full fax number.
- Can this work on a whole signature block?
- Add the m flag so ^ and $ match at the start and end of each line. Then the pattern can find the fax line inside multi-line text.