Regex Patterns for Matching Numbers Between 1 and 100: From Basic to Advanced

Nov 23, 2025 · Programming · 8 views · 7.8

Keywords: Regular Expressions | Number Matching | Range Validation

Abstract: This article provides an in-depth exploration of various regex patterns for matching numbers between 1 and 100. It begins by analyzing common mistakes in beginner patterns, then thoroughly explains the correct solution ^[1-9][0-9]?$|^100$, covering character classes, quantifiers, and grouping. The discussion extends to handling leading zeros with the more universal pattern ^0*(?:[1-9][0-9]?|100)$. Through step-by-step breakdowns and code examples, the article helps readers grasp core regex concepts while offering practical applications and performance considerations.

Regex Fundamentals and Common Pitfalls

In scenarios requiring number range matching, regular expressions offer powerful pattern matching capabilities. Many developers encounter common pitfalls when attempting to match numbers between 1 and 100. A typical erroneous pattern is ^[1-9]?[0-9]{1}$|^100$, which appears logical but contains fundamental flaws.

Let's examine the issues with this incorrect pattern: [1-9]? represents an optional digit from 1-9, while [0-9]{1} mandates exactly one digit from 0-9. This combination incorrectly matches strings like "09", which numerically equals 9 but is accepted as a valid match. More critically, this pattern fails to properly handle edge cases, such as matching single digits from 1-9.

Analysis of the Correct Solution

The optimized correct pattern is ^[1-9][0-9]?$|^100$. Let's break down each component of this expression:

First, the ^[1-9][0-9]?$ portion handles numbers from 1 to 99:

Second, the ^100$ portion specifically matches the number 100:

These two parts are connected via the | (OR operator), forming a complete matching pattern. In practical application, this pattern can be implemented as follows:

import re

pattern = r"^[1-9][0-9]?$|^100$"

test_cases = ["1", "50", "99", "100", "0", "101", "09"]
for test in test_cases:
    match = re.match(pattern, test)
    print(f"{test}: {'Match' if match else 'No match'}")

Advanced Pattern for Leading Zero Handling

In real-world applications, we often need to process numeric strings with leading zeros, such as "001", "00099", etc. To address this requirement, a more universal pattern can be used: ^0*(?:[1-9][0-9]?|100)$.

Components of this extended pattern:

Example code demonstrating this pattern's usage:

import re

advanced_pattern = r"^0*(?:[1-9][0-9]?|100)$"

test_cases_advanced = ["001", "00050", "00099", "00100", "000", "0101"]
for test in test_cases_advanced:
    match = re.match(advanced_pattern, test)
    print(f"{test}: {'Match' if match else 'No match'}")

Performance Considerations and Best Practices

Performance is a crucial factor when selecting regex patterns. The basic pattern ^[1-9][0-9]?$|^100$ offers good performance due to its simple character classes and quantifiers. The extended pattern ^0*(?:[1-9][0-9]?|100)$, which includes leading zero handling, may incur slight performance overhead when matching long strings due to the additional 0* and grouping.

In practical development, consider the following recommendations:

Practical Application Scenarios

These regex patterns find extensive application in web development, data validation, and text processing:

In form validation, these patterns can verify percentage inputs, age ranges, or other numerical constraints:

function validatePercentage(input) {
    const pattern = /^[1-9][0-9]?$|^100$/;
    return pattern.test(input);
}

// Usage examples
console.log(validatePercentage("50")); // true
console.log(validatePercentage("0"));  // false
console.log(validatePercentage("101")); // false

In data processing pipelines, these patterns can filter and extract specific numerical ranges:

import pandas as pd
import re

def filter_dataframe_by_range(df, column_name):
    pattern = r"^[1-9][0-9]?$|^100$"
    return df[df[column_name].astype(str).str.match(pattern)]

By deeply understanding the principles and applications of these regex patterns, developers can more effectively address number range matching requirements and apply them flexibly across various programming contexts.

Copyright Notice: All rights in this article are reserved by the operators of DevGex. Reasonable sharing and citation are welcome; any reproduction, excerpting, or re-publication without prior permission is prohibited.