How to write a regular expression to extract a vector ([6.300000​000000001,​0.1,-7.5]) using regexp?

8 views (last 30 days)
I have data that reads in as a 1 x 102740 char. I am trying to extract the vectors using regexp and format it as a n x 3 table.
Here is some of the data for example:
data='[{"name":"accX","units":"m/s2"},{"name":"accY","units":"m/s2"},{"name":"accZ","units":"m/s2"}],"values":[[7.4,0.2,-6.4],[6.300000000000001,0.1,-7.5],[8.1,0.8,-0.5],[9.700000000000001,2.7,0],[11.100000000000001,1.9000000000000001,-1.2000000000000002]';4
I am trying to use regexp but cannot format the expression correctly.
How can I format the expression to basically get only the vectors?
expression='^[\d*,\d*,\d*]$'; %this is what I have tried but it returns a 0x0 cell
x=regexp(data,expression,'match');
For example, from the data above, I need to only extract:
[[7.4,0.2,-6.4]
[6.300000000000001,0.1,-7.5]
[8.1,0.8,-0.5]
[9.700000000000001,2.7,0]
Ideally I would like to format the vectors into a n by 3 table with columns for x, y, and z.
So it would look something like:
x=[7.4; 6.300000000000001; 8.1; ...]
y=[0.2; 0.1; 0.8; ...]
z=-[6.4; ,-7.5; -0.5; ...]
Thank you in advance

Answers (1)

DGM
DGM on 30 Sep 2021
Edited: DGM on 30 Sep 2021
Akira's comment may be very relevant, but since I'm unfamiliar with handling JSON, I'll just throw down a regex kludge as requested:
data = '[{"name":"accX","units":"m/s2"},{"name":"accY","units":"m/s2"},{"name":"accZ","units":"m/s2"}],"values":[[7.4,0.2,-6.4],[6.300000000000001,0.1,-7.5],[8.1,0.8,-0.5],[9.700000000000001,2.7,0],[11.100000000000001,1.9000000000000001,-1.2000000000000002]';
numexpr = '[+-]?(\d*\.)?\d+';
expr = [numexpr ',' numexpr ',' numexpr];
A = regexp(data,expr,'match').';
A = str2double(split(A,','))
A = 5×3
7.4000 0.2000 -6.4000 6.3000 0.1000 -7.5000 8.1000 0.8000 -0.5000 9.7000 2.7000 0 11.1000 1.9000 -1.2000
I'm sure this could be simplified or made more robust, but eh.

Products


Release

R2021a

Community Treasure Hunt

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

Start Hunting!