How to write a regular expression to extract a vector ([6.300000000000001,0.1,-7.5]) using regexp?
8 views (last 30 days)
Show older comments
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
2 Comments
Answers (1)
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,','))
I'm sure this could be simplified or made more robust, but eh.
2 Comments
See Also
Categories
Find more on JSON Format in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!