How do I get regexp to read specific values in a text file?

1 visualización (últimos 30 días)
Thanh Le
Thanh Le el 26 de Sept. de 2019
Respondida: Abhilash Padma el 30 de Sept. de 2019
I am currently having a problem in using regular expression to extract numbers from a text file.
This is a part of a file I am working on:
---------------------------------------------------------------------
Center Atomic Atomic Coordinates (Angstroms)
Number Number Type X Y Z
---------------------------------------------------------------------
1 8 0 0.000000 -0.000000 0.114681
2 1 0 -0.000000 0.754069 -0.458726
3 1 0 -0.000000 -0.754069 -0.458726
---------------------------------------------------------------------
Rotational constants (GHZ): 858.6604959 440.9403948 291.3341325
Standard basis: 6-31G(d) (6D, 7F)
There are 10 symmetry adapted cartesian basis functions of A1 symmetry.
There are 1 symmetry adapted cartesian basis functions of A2 symmetry.
There are 3 symmetry adapted cartesian basis functions of B1 symmetry.
There are 5 symmetry adapted cartesian basis functions of B2 symmetry.
There are 10 symmetry adapted basis functions of A1 symmetry.
There are 1 symmetry adapted basis functions of A2 symmetry.
I am trying to get 3 numbers from the line starting with "Rotational constants (GHZ):".
This is my attempt:
content = fileread('myFile.txt');
pattern = 'Rotational constants (GHZ):\s*(\d+\.\d+)\s*(\d+\.\d+)\s*(\d+\.\d+)';
tokens = regexp(content, pattern, 'tokens);
values = sscanf(tokens{1}{1}, '%f');
The error I am getting is I keep getting empty array for my tokens. Based on this error, I think my regular expression is uncorrect. However, I am not exactly sure how to fix it.
Thanks

Respuestas (1)

Abhilash Padma
Abhilash Padma el 30 de Sept. de 2019
The pattern you are using involves special characters ‘(’ and ‘)’. You should use ‘\’ before special characters in pattern. The pattern should be “(?<=Rotational constants \(GHZ\):\s+)(\d+\.\d+)\s*(\d+\.\d+)\s*(\d+\.\d+)” .

Categorías

Más información sobre Characters and Strings en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by