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 237467
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T20:25:39+00:00 2026-05-11T20:25:39+00:00

Goal: Given a number (it may be very long and it is greater than

  • 0

Goal: Given a number (it may be very long and it is greater than 0), I’d like to get the five least meaningful digits dropping any 0 at the end of that number.

I tried to solve this with regex, Helped by RegexBuddy I came to this one:

[\d]+([\d]{0,4}+[1-9])0*

But python can’t compile that.

>>> import re
>>> re.compile(r"[\d]+([\d]{0,4}+[1-9])0*")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python2.5/re.py", line 188, in compile
    return _compile(pattern, flags)
  File "/usr/lib/python2.5/re.py", line 241, in _compile
    raise error, v # invalid expression
sre_constants.error: multiple repeat

The problem is the “+” after “{0,4}”, it seems it doesn’t work in python (even in 2.6)

How can I write a working regex?

PS:
I know you can start dividing by 10 and then using the remainder n%100000… but this is a problem about regex.

  • 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-05-11T20:25:39+00:00Added an answer on May 11, 2026 at 8:25 pm

    That regular expression is very superfluous. Try this:

    >>> import re
    >>> re.compile(r"(\d{0,4}[1-9])0*$")
    

    The above regular expression assumes that the number is valid (it will also match “abc0123450″, for example.) If you really need the validation that there are no non-number characters, you may use this:

    >>> import re
    >>> re.compile(r"^\d*?(\d{0,4}[1-9])0*$")
    

    Anyways, the \d does not need to be in a character class, and the quantifier {0,4} does not need to be forced to be greedy (as the additional + specifies, although apparently Python does not recognize that.)

    Also, in the second regular expression, the \d is non-greedy, as I believe this will improve the performance and accuracy. I also made it “zero or more” as I assume that is what you want.

    I also added anchors as this ensures that your regular expression won’t match anything in the middle of a string. If this is what you desired though (maybe you’re scanning a long text?), remove the anchors.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Goal... Given a Zip code and a number of miles... get all zip codes
My goal is to record the number of processor instructions executed by a given
Given a directed graph, the goal is to combine the node with the nodes
For example, my goal is to test the code given here: PHP script that
Goal I am building an Eclipse plugin targeting the 3.7 environment and would like
My goal here is to find all possible combinations that sums to a given
Is there a simple and efficient way to get the number of visitors who
My goal is to write a program that handles an arbitrary number of tasks
I'm kind of stumped on this one. What I'd like to do is get
Given a sequence of operations: a*b*a*b*a*a*b*a*b is there a way to get the optimal

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.