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!
You can do it by providing a function as the replacement:
Incidentally, if you wanted to do it using BioPython’s Newick tree parser instead, it would look like this:
(while this particular operation takes more lines than the regex version, other operations involving trees might be made much easier with it).