[Python] How to match a regular expression after another one in a text file?

Discussão em 'Python' iniciado por Stack, Setembro 26, 2024.

  1. Stack

    Stack Membro Participativo

    I have a text file whose size is about 1MB. I want to retrive some data from it, which require multiple regular expressions.

    For example, to get Atoms, Basis functions, GTFs, Total energy, Virial ratio, etc., I have to use multiple regular expressions:

    Atoms:\s+(\d+), Basis functions:\s+(\d+), GTFs:\s+(\d+), Total energy:\s+(-?\d+\.\d+), Virial ratio:\s+(-?\d+\.\d+)

    I want that, after the first regular expression is matched, the next regular expression should be matched from the point where the first regular expression is matched, and so on.

    I know the simple way is to truncate the string by text = text[match.end():] after each match, but this seems to be inefficient for a large text file.

    Is there any better way to do this?

    I find there is a question Regular expression to match text after another regex matched text for the same issue, but the answer is not satisfied.

    Converting basis function information to GTF information...
    Back converting basis function information from Cartesian to spherical type...
    Generating density matrix based on SCF orbitals...
    Generating overlap matrix...

    Total/Alpha/Beta electrons: 112.0000 56.0000 56.0000
    Net charge: 0.00000 Expected multiplicity: 1
    Atoms: 25, Basis functions: 293, GTFs: 517
    Total energy: -992.117436714606 Hartree, Virial ratio: 2.00276853
    This is a restricted single-determinant wavefunction
    Orbitals from 1 to 56 are occupied
    Title line of this file: 000001

    Loaded 000001.fchk successfully!

    Formula: H11 C9 O4 P1 Total atoms: 25
    Molecule weight: 214.15535 Da
    Point group: C1

    "q": Exit program gracefully "r": Load a new file
    ************ Main function menu ************
    0 Show molecular structure and view orbitals
    1 Output all properties at a point 2 Topology analysis
    3 Output and plot specific property in a line

    Continue reading...

Compartilhe esta Página