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

1 次查看(过去 30 天)
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

回答(1 个)

Abhilash Padma
Abhilash Padma 2019-9-30
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+)” .

类别

Help CenterFile Exchange 中查找有关 Characters and Strings 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by