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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T05:21:39+00:00 2026-06-17T05:21:39+00:00

Is it possible to perform simple math on the output from Python regular expressions?

  • 0

Is it possible to perform simple math on the output from Python regular expressions?

I have a large file where I need to divide numbers following a ")" by 100. For instance, I would convert the following line containing )75 and )2:

((words:0.23)75:0.55(morewords:0.1)2:0.55);

to )0.75 and )0.02:

((words:0.23)0.75:0.55(morewords:0.1)0.02:0.55);

My first thought was to use re.sub using the search expression "\)\d+", but I don’t know how to divide the integer following the parenthesis by 100, or if this is even possible using re.

Any thoughts on how to solve this? Thanks for your help!

  • 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-17T05:21:40+00:00Added an answer on June 17, 2026 at 5:21 am

    You can do it by providing a function as the replacement:

    s = "((words:0.23)75:0.55(morewords:0.1)2:0.55);"
    
    s = re.sub("\)(\d+)", lambda m: ")" + str(float(m.groups()[0]) / 100), s)
    
    print s
    # ((words:0.23)0.75:0.55(morewords:0.1)0.02:0.55);
    

    Incidentally, if you wanted to do it using BioPython’s Newick tree parser instead, it would look like this:

    from Bio import Phylo
    # assuming you want to read from a string rather than a file
    from StringIO import StringIO
    
    tree = Phylo.read(StringIO(s), "newick")
    
    for c in tree.get_nonterminals():
        if c.confidence != None:
            c.confidence = c.confidence / 100
    
    print tree.format("newick")
    

    (while this particular operation takes more lines than the regex version, other operations involving trees might be made much easier with it).

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

Sidebar

Related Questions

I want to perform a hidden HTTP-GET request from MS-access, as simple as possible,
Possible Duplicate: working with incredibly large numbers in .NET I'm working on a simple
Is it possible to perform a global reversed-find on NHibernate-managed objects? Specifically, I have
I have a simple dot diagram to show how to perform tests. PerformTests; PerformTests<---+
So what I need is simple: each time we perform our shader (meaning on
I have 2 simple tables that I would like to perform an INNER JOIN
Is it possible to perform a group by on an object? from item in
How is it possible to make live-search with hashes? Now I have a simple
Possible Duplicate: How can I format numbers as money in JavaScript? I have a
Is it possible to perform a partial or patch commit in Team Foundation? I

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.