Regex to Match the first 6 digits of a phone number

Learn regex to match the first 6 digits of a phone number with practical examples, diagrams, and best practices. Covers regex development techniques with visual explanations.

Regex to Match the First 6 Digits of a Phone Number

Hero image for Regex to Match the first 6 digits of a phone number

Learn how to construct regular expressions to accurately extract or validate the initial six digits of various phone number formats.

Extracting specific parts of a phone number, such as the first six digits, is a common task in data processing, validation, and analysis. This article will guide you through creating robust regular expressions (regex) that can handle different phone number formats, including those with country codes, area codes, and various separators. We'll cover the fundamental concepts and provide practical examples to help you match exactly what you need.

Understanding Phone Number Formats

Phone numbers come in many shapes and sizes. Before we can match the first six digits, it's crucial to understand the common patterns. These often include an optional country code, an area code, and then the local number. The 'first six digits' can refer to different parts depending on whether a country code is present or if you're only interested in the local number's prefix.

flowchart TD
    A[Start] --> B{Phone Number Format?}
    B -->|International| C[+CC (2-3 digits)]
    B -->|National| D[No Country Code]
    C --> E[Area Code (3 digits)]
    D --> E
    E --> F[Prefix (3 digits)]
    F --> G[Line Number (4 digits)]
    G --> H[End]
    style A fill:#f9f,stroke:#333,stroke-width:2px
    style H fill:#f9f,stroke:#333,stroke-width:2px

Common Phone Number Structure Flow

For the purpose of matching the 'first 6 digits', we generally mean the first six numerical characters encountered, ignoring any non-digit separators like spaces, hyphens, or parentheses. However, sometimes you might need to be more specific, for instance, matching the first six digits after an optional country code.

Basic Regex for Any First 6 Digits

The simplest approach to capture the first six digits, regardless of their position or surrounding characters, is to use \D* to match any non-digit characters (zero or more times) followed by \d{6} to match exactly six digits. The ^ anchor ensures we start from the beginning of the string.

^\D*(\d{6})

Regex to capture the first six digits, ignoring leading non-digits.

Let's break this down:

  • ^: Asserts position at the start of the string.
  • \D*: Matches any non-digit character (like spaces, hyphens, parentheses) zero or more times. This allows us to skip initial formatting.
  • (\d{6}): This is the capturing group. \d matches any digit (0-9), and {6} specifies that it must match exactly six of them. The parentheses () make these six digits a capturing group, so you can easily extract them.

Handling Specific Phone Number Formats

Often, phone numbers follow more structured patterns. We can refine our regex to account for optional country codes, area codes, and various separators. Here are a few common scenarios:

US/Canada Format (10 digits)

For a 10-digit number like (123) 456-7890 or 123-456-7890, where we want the first six digits of the local number (e.g., 123456).

^(?:\+?\d{1,3}[\s.-]?)?\(?(\d{3})\)?[\s.-]?(\d{3})\d{4}$

Explanation:

  • ^(?:\+?\d{1,3}[\s.-]?)?: Optionally matches an international code (e.g., +1) and its separator.
  • \(?(\d{3})\)?[\s.-]?: Matches an optional opening parenthesis, captures the first three digits (area code), an optional closing parenthesis, and an optional separator.
  • (\d{3}): Captures the next three digits (the prefix).
  • \d{4}$: Matches the remaining four digits and asserts the end of the string.

To get the first six digits, you would concatenate the content of the first and second capturing groups.

International Format (Variable Length)

For numbers that might start with a country code, and you want the first six digits after the country code.

^\+?\d{1,4}[\s.-]?(\d{6})

Explanation:

  • ^\+?: Optionally matches a leading +.
  • \d{1,4}: Matches 1 to 4 digits for the country code.
  • [\s.-]?: Optionally matches a space, hyphen, or dot as a separator.
  • (\d{6}): Captures the next six digits, which would be the start of the national number.

Strictly First 6 Digits (No Separators)

If you only care about the very first six digits, assuming the string starts with digits or you've pre-processed it to remove non-digits.

^(\d{6})

Explanation:

  • ^: Asserts position at the start of the string.
  • (\d{6}): Captures exactly the first six digits.

Implementation Example (JavaScript)

Here's how you might use one of these regular expressions in a programming language like JavaScript to extract the first six digits.

function getFirstSixDigits(phoneNumber) {
  // Regex to capture the first six digits, ignoring leading non-digits
  const regex = /^\D*(\d{6})/;
  const match = phoneNumber.match(regex);

  if (match && match[1]) {
    return match[1]; // The captured group
  } else {
    return null; // Or throw an error, depending on desired behavior
  }
}

console.log(getFirstSixDigits("+1 (555) 123-4567")); // Expected: "555123"
console.log(getFirstSixDigits("555-123-4567"));      // Expected: "555123"
console.log(getFirstSixDigits("1234567890"));        // Expected: "123456"
console.log(getFirstSixDigits("abc-123"));           // Expected: null (less than 6 digits)
console.log(getFirstSixDigits("NoDigitsHere"));      // Expected: null

JavaScript function to extract the first six digits using regex.

This example demonstrates how to use the match() method in JavaScript, which returns an array containing the full match and any captured groups. The first captured group (match[1]) holds the six digits we're interested in.