MATLAB Answers

Thanh Le
0

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

Asked by Thanh Le
on 26 Sep 2019
Latest activity Answered by Abhilash Padma on 30 Sep 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

  0 Comments

Sign in to comment.

1 Answer

Answer by Abhilash Padma on 30 Sep 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+)” .

  0 Comments

Sign in to comment.