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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T14:46:10+00:00 2026-05-14T14:46:10+00:00

Can regular expressions be used to perform arithmetic? Such as find all numbers in

  • 0

Can regular expressions be used to perform arithmetic? Such as find all numbers in a file and multiply them by a scalar value.

  • 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-14T14:46:10+00:00Added an answer on May 14, 2026 at 2:46 pm

    I prepared a small script which uses re.finditer to find all the integers (you can change the regexp so that it can deal with floats or scientific notation) and then use map to return a list of scaled numbers.

    import re
    
    def scale(fact):
        """This function returns a lambda which will scale a number by a                           
        factor 'fact'"""
        return lambda val: fact * val
    
    def find_and_scale(file, fact):
        """This function will find all the numbers (integers) in a file and                        
        return a list of all such numbers scaled by a factor 'fact'"""
        num = re.compile('(\d+)')
        scaling = scale(fact)
        f = open(file, 'r').read()
        numbers = [int(m.group(1)) for m in num.finditer(f)]
        return map(scaling, numbers)
    
    if __name__ == "__main__":
        import sys
        if len(sys.argv) != 3:
            print "usage: %s file factor" % sys.argv[0]
            sys.exit(-1)
        numbers = find_and_scale(sys.argv[1], int(sys.argv[2]))
        for number in numbers:
            print "%d " % number
    

    If you have a file whose numbers you want to scale by a factor fact, you call the script from the command line as python script.py file fact and it will print to STDOUT all the scaled numbers. Of course, you can do something more useful if you wanted…

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

Sidebar

Related Questions

I know regular expressions can be used, but am not able to find the
Regular expressions can become quite complex. The lack of white space makes them difficult
I need to find attribute values in an ASPX file using regular expressions. That
what regular expressions are used for email validation can you help me out $(Textbox).rules(add,
As explained in Can regular expressions be used to match nested patterns? , it
Can a regular expression be used inside a stored procedure? If it can, how?
I'm developing an application in .NET where the user can provide Regular Expressions that
Are Regular Expressions useful for a Web Designer (XHTML/CSS)? Can a web designer get
How can I validate a string using Regular Expressions to only allow alphanumeric characters
I can't get my head around how regular expressions are done and am trying

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.