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
0 件のコメント
回答 (1 件)
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+)” .
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Characters and Strings についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!