[Tutor] reg exps (fwd)

Michael Janssen Janssen at rz.uni-frankfurt.de
Thu Feb 5 09:51:08 EST 2004


On Thu, 5 Feb 2004, Kim Branson wrote:

> i have a program which will spit out data like so:
> % ~/Desktop/Dock4_osx/bin/scorer 1rx3.pdb dock_nrg.mol2
>   PK=  6.08 Qual=  2.12 PMF= -159.15 PMF_rb= -144.15 SMoG= -161.27
> SMoG_H= -7.68 ChemScore= -23.86 Clash=  0.85 Int=  2.58 DockNRG= -24.51
> AutoDock= -20.14

>      score_lines = re.compile('PK= (.*) Qual= (.*) PMF= (.*) PMF_rb=
> (.*) SMoG= (.*) SMoG_H= (.*) ChemScore= (.*) Clash= (.*) Int= (.*)
> DockNRG= (.*) AutoDock= (.*)')

The regular expression seems a bit to long for my taste. When you only
want to retrieve the numerical values you can much easier do:

# untestet
reg_values = re.compile('[-0-9.]+')
values = re.findall(reg_values, line)

for name, val in zip(a_list_of_all_names_in_order, values):
   print name, val

A long regexp is bad, when something in the line changes (you wont' get
any output at all then). OTOH Karl's suggestions with named groups has
the advantages that you only need to change the regexp, when the output
changes.


Michael



More information about the Tutor mailing list