I'm new to this website. Here's a problem that troubled me for >2 hr. I have a string (phylogenetic tree in newick format), which looks like:
((A:14,B:43):22,C:76,(D:54,(E:87,F:28):17):35);
The tree may have multiple levels, indicated by parentheses. Now I want to add a number, say, 10, to the top level numbers (branch lengths). Here there are only three top level numbers: 22, 76, 35. After the convertion the string should look like:
((A:14,B:43):32,C:86,(D:54,(E:87,F:28):17):45);
I have tried my best thinking out a proper regex, but finally admitted my limitation. How can it be done really?
s/(?:^\(|(\((?:(?>[^()]*)|(?1))*\)))\K|:\K([0-9]+)/$2?$2+10:""/ge
Match either things you want to skip or digits preceded by a :.
Things you want to skip are either the leading (
or any balanced set of parentheses (balanced parentheses regex taken almost literally from perlre).
In the substitution, add ten if digits to be modified were matched, otherwise match nothing.
But you are better off not being clever and instead going to the work to parse, modify, and reserialize your tree.