Check if a String Starts With Number Using Regular Expression

Better Stack Team
Updated on November 18, 2024

To check if a string starts with a number using a regular expression (regex), you can use a pattern that matches the beginning of the string (^) followed by any digit (\\d). Here’s how you can do this in various programming languages and environments.

Regex Pattern

The regex pattern to match a string that starts with a number is:

 
^\\d
  • ^ asserts the position at the start of the string.
  • \\d matches any digit (equivalent to [0-9]).

Examples in Different Languages

1. JavaScript

 
const str = "123abc";
const regex = /^\\d/;

if (regex.test(str)) {
    console.log("The string starts with a number.");
} else {
    console.log("The string does not start with a number.");
}

2. Python

 
import re

str = "123abc"
pattern = r'^\\d'

if re.match(pattern, str):
    print("The string starts with a number.")
else:
    print("The string does not start with a number.")

3. Java

 
public class Main {
    public static void main(String[] args) {
        String str = "123abc";
        String pattern = "^\\\\d";

        if (str.matches(pattern)) {
            System.out.println("The string starts with a number.");
        } else {
            System.out.println("The string does not start with a number.");
        }
    }
}

4. PHP

 
$str = "123abc";
$pattern = '/^\\d/';

if (preg_match($pattern, $str)) {
    echo "The string starts with a number.";
} else {
    echo "The string does not start with a number.";
}

5. C#

 
using System;
using System.Text.RegularExpressions;

class Program {
    static void Main() {
        string str = "123abc";
        string pattern = @"^\\d";

        if (Regex.IsMatch(str, pattern)) {
            Console.WriteLine("The string starts with a number.");
        } else {
            Console.WriteLine("The string does not start with a number.");
        }
    }
}

Summary

The regex pattern ^\\d is effective for checking if a string starts with a number across various programming languages. You can easily adapt the examples provided to fit your specific needs. This approach is useful in data validation, parsing, and processing tasks where identifying the initial character of a string is essential.

Got an article suggestion? Let us know
Explore more
Licensed under CC-BY-NC-SA

This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.

Make your mark

Join the writer's program

Are you a developer and love writing and sharing your knowledge with the world? Join our guest writing program and get paid for writing amazing technical guides. We'll get them to the right readers that will appreciate them.

Write for us
Writer of the month
Marin Bezhanov
Marin is a software engineer and architect with a broad range of experience working...
Build on top of Better Stack

Write a script, app or project on top of Better Stack and share it with the world. Make a public repository and share it with us at our email.

community@betterstack.com

or submit a pull request and help us build better products for everyone.

See the full list of amazing projects on github