Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8627325
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T08:15:03+00:00 2026-06-12T08:15:03+00:00

I want matched only the strings that represent numbers between 0…9999 import re NUMERIC

  • 0

I want matched only the strings that represent numbers between 0...9999

import re
NUMERIC  = re.compile("\d{,4}")
NUMERIC.match("324234")
nr =NUMERIC.match("324234")
nr.group(0)

Tried the above but it matches the first 4 digits from the string, even if the string has 5 digits.

Regex to match the numbers that have between 1 and 4 digits from this string represention of an integer number?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T08:15:04+00:00Added an answer on June 12, 2026 at 8:15 am

    Anchors do the trick of not matching too much:

    >>> pattern = re.compile("^\d{1,4}$")
    >>> pattern.match("0").group()
    '0'
    >>> pattern.match("42").group()
    '42'
    >>> pattern.match("777").group()
    '777'
    >>> pattern.match("2012").group()
    '2012'
    >>> pattern.match("65535").group()
    ------------------------------------------------------------
    Traceback (most recent call last):
      File "<ipython console>", line 1, in <module>
    AttributeError: 'NoneType' object has no attribute 'group'
    

    Note the {1,4} — I’m assuming you don’t want to match the empty string. However, this will not match 00001, which certainly is in range.

    A more robust alternative to regular expressions is to leverage Python’s built-in integer parsing:

    def parse_4digit_number(s):
        i = int(s)
        if 0 <= i <= 9999:
            return i
        else:
            raise ValueError("{0} is out of range".format(i))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to match all URLS but exclude image urls from beeing matched with
I am trying to use grep to match lines that contain two different strings.
I fairly frequently match strings against regular expressions. In Java: java.util.regex.Pattern.compile(\w+).matcher(this_is).matches Ouch. Scala has
I want a regex to match words that are delimited by double or more
I want to match the e in these strings: e e-x-ex e-x e-ex However,
In an online manager game (like Hattrick ), I want to simulate matches between
I want to create a regexp that matches exactly the string foo , even
I want to make a regular expression that matches the form (+92)-(21)-1234... . I
I need a regex that matches the following (the brackets indicate I want to
I have several dataframes in which I want to delete each row that matches

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.