Reputation: 2327
How can I write a regular expression to return the value of the mesh volume? The file containing this information is printed below.
Mesh Bounding Box Size 13119.671875 13057.258789 5996.836426
Mesh Bounding Box Diag 19457.128906
Mesh Bounding Box min -6823.634277 -6530.717773 0.000000
Mesh Bounding Box max 6296.037598 6526.541016 5996.836426
Mesh Surface Area is 373208000.000000
Mesh Volume is 48660.9190912000000
Mesh Total Len of 2430 Edges is 1946449.875000 Avg Len 801.008179
Mesh Total Len of 2430 Edges is 1946449.875000 Avg Len 801.008179 (including faux edges))
Thin shell (faces) barycenter: -228.323471 -0.174702 1865.262939
Vertices barycenter -175.590256 7.494401 2809.697754
I am able to get the volume of the mesh (which is 48660.9190912000000
) using the following:
\d+[.]*\d+$
but it also matches all the other numbers. I tried
(Mesh Volume is)\w+\d+[.]*\d+$
but it fails to find any match. Could someone help me with that?
If I group the numbering as the following, can I return the value of the volume as \1
?
(Mesh Volume is)\w+(\d+[.]*\d+)$
My goal is to find a working regular expression to use in MATLAB regexp
function.
Upvotes: 2
Views: 82
Reputation: 112679
You can use lookbehind as follows. Note also that
\.
, not .
Code in Matlab:
s = 'Mesh Surface Area is 373208000.000000 Mesh Volume is 48660.9190912000000'; % example
result = regexp(s, '(?<=Mesh Volume is )\d+\.?\d+', 'match');
Upvotes: 1
Reputation: 492
You could try something like this:
%float_pattern = "[0-9.]+";
float_pattern = "\d+(?:\.\d+)?";
pattern = "Mesh Volume is (?<MeshVolume>" + float_pattern + ")";
matches = regexp(information, pattern, 'match')
tokens = regexp(information, pattern, 'tokens')
names = regexp(information, pattern, 'names')
if isfield(names, "MeshVolume")
fprintf("Mesh Volume = %f m^3\n", names.MeshVolume);
else
fprintf("Failed to find mesh volume.\n");
end
Upvotes: 2
Reputation: 163362
If you want to grab the number, you could use the capturing group for the number instead of the text.
For your example data, you can omit \w+
as the number directly follows is
.
Note that the pattern you tried \d+[.]*\d+$
might also match 1..1
. When there is no dot it requires at least 2 digits due to the two times \d+
.
You could use:
Mesh Volume is (\d+(?:\.\d+)?)
Explanation
Mesh Volume is
Match literally(
Capture group 1
\d+
Match 1+ digits(?:\.\d+)?
Optionally ?
match a single dot followed by 1+ digits)
Close group 1Upvotes: 2